package com.itant.shiwushu.net;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
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.SharedPreferenceManager;
import com.itant.shiwushu.manager.ToastManager;
import com.itant.shiwushu.manager.UserManager;
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/10/7.
 */

public class GlobalPresenter {
    private static boolean isReport = false;
    public static void submitFeedback(final String feedback, String reporterId, String reporterName, String authorId, String authorName) {
        UserManager userManager = UserManager.getInstance();
        String email = userManager.getUser().getEmail();
        //创建表单请求体
        FormBody.Builder formBody = new FormBody.Builder();
        String realFeedBack = feedback;
        if (!TextUtils.isEmpty(reporterId)) {
            isReport = true;
            realFeedBack = reporterName + "【" + reporterId + "】举报" + authorName + "【" + authorId + "】：" + feedback;
        } else {
            isReport = false;
        }
        //传递键值对参数
        formBody.add("userId", userManager.getUser().getUserId());
        formBody.add("email", TextUtils.isEmpty(email) ? "" : email);
        formBody.add("feedback", realFeedBack);
        //创建Request 对象
        Request request = new Request.Builder().url(ServerHost.URL_FEEDBACK)
                //传递请求体
                .post(formBody.build()).build();
        OkHttpClient okHttpClient = RequestClient.getClient().getInterceptOkHttpClient();
        okHttpClient.newCall(request).enqueue(new ElasticCallback() {

            @Override
            public void onRequestFailure(Call call, IOException e) {
                // 反馈失败
                sendFeedBackResult(false, feedback, isReport);
            }

            @Override
            public void onRequestResponse(int code, String result) throws IOException {
                if (code == 200) {
                    ResponseResult responseResult = JSON.parseObject(result, ResponseResult.class);
                    if (responseResult != null) {
                        if (responseResult.getResultCode() == ResultCode.SUCCESS) {
                            // 反馈成功
                            sendFeedBackResult(true, feedback, isReport);
                            return;
                        }
                    }
                }
                // 反馈失败
                sendFeedBackResult(false, feedback, isReport);
            }

            @Override
            public void onRequestComplete() {

            }
        });
    }

    private static void sendFeedBackResult(boolean success, String message, boolean isReport) {
        if (success) {
            if (!isReport) {
                // 反馈成功，清空临时内容
                SharedPreferenceManager.getInstance().putString(SharedPreferenceManager.KEY_FEEDBACK, "");
            }

            ToastManager.getInstance().toastShort("反馈发送成功");
        } else {
            if (!isReport) {
                // 反馈失败，保存发送失败的内容
                SharedPreferenceManager.getInstance().putString(SharedPreferenceManager.KEY_FEEDBACK, message);
            }
            ToastManager.getInstance().toastShort("反馈发送失败");
        }
    }

    /**
     * 成为VIP用户
     */
    public static void becomeVIP() {
        User user = UserManager.getInstance().getUser();
        //创建表单请求体
        FormBody.Builder formBody = new FormBody.Builder();
        //传递键值对参数
        formBody.add("userId", user.getUserId());
        formBody.add("token", user.getToken());
        //创建Request 对象
        Request request = new Request.Builder().url(ServerHost.URL_VIP)
                //传递请求体
                .post(formBody.build()).build();
        OkHttpClient okHttpClient = RequestClient.getClient().getInterceptOkHttpClient();
        okHttpClient.newCall(request).enqueue(new ElasticCallback() {

            @Override
            public void onRequestFailure(Call call, IOException e) {
                // 成为VIP失败
                e.printStackTrace();
            }

            @Override
            public void onRequestResponse(int code, String result) throws IOException {
                if (code == 200) {
                    ResponseResult<User> responseResult = JSON.parseObject(result, new TypeReference<ResponseResult<User>>(){});
                    if (responseResult != null) {
                        if (responseResult.getResultCode() == ResultCode.SUCCESS) {
                            // 成为VIP成功
                            UserManager.getInstance().notifyUserDataUpdate(responseResult.getResultObj());
                            return;
                        }
                    }
                }
                // 成为VIP失败
            }

            @Override
            public void onRequestComplete() {

            }
        });
    }
}
