package com.itant.shiwushu.ui.menu.sub;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.itant.shiwushu.bean.LostFound;
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 java.util.List;

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

/**
 * 获取帖子
 * Created by Jason on 2018/10/8.
 */

public class PostPresenter implements PostContract.Presenter {

    private PostContract.View view;

    public PostPresenter(PostContract.View view) {
        this.view = view;
    }

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

    @Override
    public void getPost(int cityId, int postType, int lostType, String keywords, int page) {
        User user = UserManager.getInstance().getUser();

        //创建表单请求体
        FormBody.Builder registerBody = new FormBody.Builder();
        //传递键值对参数
        registerBody.add("userId", user.getUserId());
        registerBody.add("token", user.getToken());
        registerBody.add("cityId", String.valueOf(cityId));
        registerBody.add("postType", String.valueOf(postType));
        registerBody.add("lostType", String.valueOf(lostType));
        registerBody.add("keywords", keywords);
        registerBody.add("page", String.valueOf(page));

        OkHttpClient client = RequestClient.getClient().getNormalOkHttpClient();
        //创建Request 对象
        Request request = new Request.Builder()
                .url(ServerHost.URL_GET_POST)
                //传递请求体
                .post(registerBody.build())
                .build();
        client.newCall(request).enqueue(new ElasticCallback() {

            @Override
            public void onRequestFailure(Call call, IOException e) {
                e.printStackTrace();
                if (view != null) {
                    view.onGetPostFailed("获取失败");
                }
            }

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

                if (view != null) {
                    view.onGetPostFailed("获取失败");
                }
            }

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

    @Override
    public void deletePost(int postId, String authorId) {
        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));
        registerBody.add("authorId", authorId);

        OkHttpClient client = RequestClient.getClient().getNormalOkHttpClient();
        //创建Request 对象
        Request request = new Request.Builder()
                .url(ServerHost.URL_DELETE_POST)
                //传递请求体
                .post(registerBody.build())
                .build();
        client.newCall(request).enqueue(new ElasticCallback() {

            @Override
            public void onRequestFailure(Call call, IOException e) {
                e.printStackTrace();
                if (view != null) {
                    view.onDeleteFailed("删除失败");
                }
            }

            @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.onDeleteSuccess("删除成功");
                            return;
                        } else {
                            view.onGetPostFailed(responseResult.getMessage());
                            return;
                        }
                    }
                }

                if (view != null) {
                    view.onDeleteFailed("删除失败");
                }
            }

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