package com.sheep.gamegroup.presenter;

import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.sheep.gamegroup.model.api.ApiService;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.gamegroup.util.ChannelContent;
import com.sheep.gamegroup.util.PreferenceUtils;
import com.sheep.gamegroup.util.UMConfigUtils;
import com.sheep.jiuyan.samllsheep.SheepApp;


import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by ljy on 2018/3/15.
 */

public class PhonePresenter implements PhoneContract.Presenter {
    private PhoneContract.View view;
    private ApiService apiService;
    private boolean isOk=true;

    @Inject
    public PhonePresenter(PhoneContract.View view, ApiService apiService) {
        this.view = view;
        this.apiService = apiService;
    }

    @Override
    public void getCaptcha(String telphone) {
        try {
            Map map = new HashMap();
            map.put("account", telphone);
            Log.e("hash-map", map.toString());
            com.alibaba.fastjson.JSONObject j = new com.alibaba.fastjson.JSONObject();
            j.put("account", telphone);

            apiService.getCaptcha(j)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                        @Override
                        public void onError(BaseMessage baseMessage) {
                            //LogUtil.logI(new Gson().toJson(baseMessage));
                            view.gaptchaFail(baseMessage);
                        }

                        @Override
                        public void onNext(BaseMessage baseMessage) {
                            //LogUtil.logI(new Gson().toJson(baseMessage));
                            view.returnGaptcha(baseMessage);

                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    /**
     * @param telphone  手机号
     * @return  true 获取验证码成功，false获取验证码失败
     */
    @Override
    public boolean getCaptchaa(String telphone) {

        try {
            com.alibaba.fastjson.JSONObject j = new com.alibaba.fastjson.JSONObject();
            j.put("account", telphone);

            apiService.getCaptcha(j)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                        @Override
                        public void onError(BaseMessage baseMessage) {
                            isOk = false;
                            view.gaptchaFail(baseMessage);
                            //LogUtil.logE(new Gson().toJson(baseMessage));
                        }

                        @Override
                        public void onNext(BaseMessage baseMessage) {
                            isOk = true;
                            view.returnGaptcha(baseMessage);
                            //LogUtil.logI(new Gson().toJson(baseMessage));
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }
        //LogUtil.logI("isOk=="+isOk);
        return isOk;

    }

    @Override
    public void loginByCaptcha(String telphone, String code) {
        com.alibaba.fastjson.JSONObject m = new com.alibaba.fastjson.JSONObject();
        m.put("account", telphone);
        m.put("sec_code", code);
        m.put("invitation_code", ChannelContent.getInstance().getChannel_name());
        PreferenceUtils.setPrefString(SheepApp.getInstance(), UMConfigUtils.LOGIN_TYPE, UMConfigUtils.Source.SHEEP);
        apiService.loginByCaptcha(m)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                    @Override
                    public void onError(BaseMessage baseMessage) {
                        view.loginFail(baseMessage);

                    }

                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        view.returnLogindata(baseMessage);

                    }
                });

    }

    @Override
    public void switchPhone(JSONObject object) {
        apiService.switchPhone(object)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                    @Override
                    public void onError(BaseMessage baseMessage) {
                        view.changeTelFail(baseMessage);
                    }

                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        view.changeTelSuccess(baseMessage);
                    }
                });
    }

    @Override
    public void bindPhone(JSONObject object) {
        apiService.bindPhone(object)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                    @Override
                    public void onError(BaseMessage baseMessage) {
                        view.bindTelFail(baseMessage);
                    }

                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        view.bindTelSuccess(baseMessage);
                    }
                });
    }

    @Override
    public void smsBindMobile(JSONObject object) {
        apiService.smsBindMobile(object)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                    @Override
                    public void onError(BaseMessage baseMessage) {
                        view.bindTelSmsFail(baseMessage);
                    }

                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        view.bindTelSmsSuccess(baseMessage);
                    }
                });
    }
}
