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 rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

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

public class PhonePresenter implements PhoneContract.Presenter {
    private PhoneContract.View view;
    private ApiService apiService;

    @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.mContext) {
                        @Override
                        public void onError(BaseMessage baseMessage) {
                            view.gaptchaFail(baseMessage);
                        }

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

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


    }

    @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.mContext, UMConfigUtils.LOGIN_TYPE,UMConfigUtils.Source.SHEEP);
        apiService.loginByCaptcha(m)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.mContext) {
                    @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.mContext) {
                    @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.mContext) {
                    @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.mContext) {
                    @Override
                    public void onError(BaseMessage baseMessage) {
                        view.bindTelSmsFail(baseMessage);
                    }

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