package com.itant.shiwushu.ui.detail;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.itant.shiwushu.bean.FavoriteResult;
import com.itant.shiwushu.bean.ResponseResult;
import com.itant.shiwushu.bean.User;
import com.itant.shiwushu.constant.ResultCode;
import com.itant.shiwushu.constant.ServerHost;
import com.itant.shiwushu.manager.UserManager;
import com.itant.shiwushu.net.ElasticCallback;
import com.itant.shiwushu.tool.RequestClient;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;

/**
 *
 * Created by Jason on 2018/8/30.
 */

public class FavoritePresenter implements FavoriteContract.Presenter {
    private FavoriteContract.View view;

    public FavoritePresenter(FavoriteContract.View view) {
        this.view = view;
    }

    public void setView(FavoriteContract.View view) {
        this.view = view;
    }

    @Override
    public void getFavorite(int postId) {
        User user = UserManager.getInstance().getUser();
        //创建表单请求体
        FormBody.Builder registerBody = new FormBody.Builder();
        //传递键值对参数
        registerBody.add("userId", user.getUserId());
        registerBody.add("token", user.getToken());
        registerBody.add("postId", String.valueOf(postId));

        OkHttpClient registerClient = RequestClient.getClient().getNormalOkHttpClient();
        //创建Request 对象
        Request registerRequest = new Request.Builder()
                .url(ServerHost.URL_IS_FAVORITE)
                //传递请求体
                .post(registerBody.build())
                .build();

        registerClient.newCall(registerRequest).enqueue(new ElasticCallback() {

            @Override
            public void onRequestFailure(Call call, IOException e) {
                if (view != null) {
                    view.onGetFavoriteFailed("网络不稳定");
                }
            }

            @Override
            public void onRequestResponse(int code, String result) throws IOException {
                if (code == 200) {
                    ResponseResult<FavoriteResult> responseResult = JSON.parseObject(result, new TypeReference<ResponseResult<FavoriteResult>>(){});
                    if (responseResult != null && view != null) {
                        if (responseResult.getResultCode() == ResultCode.SUCCESS) {
                            FavoriteResult favoriteResult = responseResult.getResultObj();
                            view.onGetFavoriteSuccess(favoriteResult);
                            return;
                        } else {
                            view.onGetFavoriteFailed(responseResult.getMessage());
                            return;
                        }
                    }
                }

                if (view != null) {
                    view.onGetFavoriteFailed("查询失败");
                }
            }

            @Override
            public void onRequestComplete() {
                if (view != null) {
                    view.onRequestComplete();
                }
            }
        });
    }

    @Override
    public void goFavorite(int postId) {
        User user = UserManager.getInstance().getUser();
        //创建表单请求体
        FormBody.Builder registerBody = new FormBody.Builder();
        //传递键值对参数
        registerBody.add("userId", user.getUserId());
        registerBody.add("token", user.getToken());
        registerBody.add("postId", String.valueOf(postId));

        OkHttpClient registerClient = RequestClient.getClient().getNormalOkHttpClient();
        //创建Request 对象
        Request registerRequest = new Request.Builder()
                .url(ServerHost.URL_FAVORITE)
                //传递请求体
                .post(registerBody.build())
                .build();

        registerClient.newCall(registerRequest).enqueue(new ElasticCallback() {

            @Override
            public void onRequestFailure(Call call, IOException e) {
                if (view != null) {
                    view.goFavoriteFailed("操作失败");
                }
            }

            @Override
            public void onRequestResponse(int code, String result) throws IOException {
                if (code == 200) {
                    ResponseResult<String> responseResult = JSON.parseObject(result, new TypeReference<ResponseResult<String>>(){});
                    if (responseResult != null && view != null) {
                        if (responseResult.getResultCode() == ResultCode.SUCCESS) {
                            view.goFavoriteSuccess(responseResult.getMessage());
                            return;
                        } else {
                            view.goFavoriteFailed(responseResult.getMessage());
                            return;
                        }
                    }
                }

                if (view != null) {
                    view.goFavoriteFailed("操作失败");
                }
            }

            @Override
            public void onRequestComplete() {
                if (view != null) {
                    view.onRequestComplete();
                }
            }
        });
    }
}
