package com.itant.shiwushu.ui.login;

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/10/5.
 */

public class LoginPresenter implements LoginContract.Presenter {
    private LoginContract.View view;

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

    public LoginPresenter(LoginContract.View view) {
        this.view = view;
    }

    @Override
    public void qqLogin(String openId, String token, String nickName, String headIconUrl) {
        //创建表单请求体
        FormBody.Builder registerBody = new FormBody.Builder();
        //传递键值对参数
        registerBody.add("userId", openId);
        registerBody.add("token", token);
        registerBody.add("nickName", nickName);
        registerBody.add("headIcon", headIconUrl);

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

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

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

            @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) {
                            User user = responseResult.getResultObj();
                            view.onQQLoginSuccess(user);
                            return;
                        } else {
                            view.onQQLoginFailed(responseResult.getMessage());
                            return;
                        }
                    }
                }

                if (view != null) {
                    view.onQQLoginFailed("登录失败");
                }
            }

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

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

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

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

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

            @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) {
                            User user = responseResult.getResultObj();
                            view.onQQLoginSuccess(user);
                            return;
                        } else {
                            view.onQQLoginFailed(responseResult.getMessage());
                            return;
                        }
                    }
                }

                if (view != null) {
                    view.onQQLoginFailed("登录失败");
                }
            }

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