package com.itant.shiwushu.ui.forget;

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.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 ForgetPresenter implements ForgetContract.Presenter {
    private ForgetContract.View view;

    public ForgetPresenter(ForgetContract.View view) {
        this.view = view;
    }

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

    @Override
    public void getCode(String email) {

        //创建表单请求体
        FormBody.Builder formBody = new FormBody.Builder();
        //传递键值对参数
        formBody.add("email", email);
        //创建Request 对象
        Request request = new Request.Builder().url(ServerHost.URL_GET_CODE)
                //传递请求体
                .post(formBody.build()).build();
        OkHttpClient okHttpClient = RequestClient.getClient().getNormalOkHttpClient();
        okHttpClient.newCall(request).enqueue(new ElasticCallback() {

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

            @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) {
                            if (view != null) {
                                view.onGetCodeSuccess("获取成功");
                            }
                            return;
                        } else {
                            if (view != null) {
                                view.onGetCodeFailed(responseResult.getMessage());
                            }
                            return;
                        }
                    }
                }
                if (view != null) {
                    view.onGetCodeFailed("获取验证码失败");
                }
            }

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

    @Override
    public void resetPassword(String email, String code, String password) {
        //创建表单请求体
        FormBody.Builder registerBody = new FormBody.Builder();
        //传递键值对参数
        registerBody.add("email", email);
        registerBody.add("code", code);
        registerBody.add("password", password);

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

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

            @Override
            public void onRequestFailure(Call call, IOException e) {
                if (view != null) {
                    view.onResetFailed("重设密码失败");
                }
            }

            @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 && view != null) {
                        if (responseResult.getResultCode() == ResultCode.SUCCESS) {
                            view.onResetSuccess("密码重设成功");

                            return;
                        } else {
                            view.onResetFailed(responseResult.getMessage());
                            return;
                        }
                    }
                }

                if (view != null) {
                    view.onResetFailed("重设密码失败");
                }
            }

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