package com.sheep.gamegroup.module.login;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.sheep.gamegroup.absBase.BaseActivity;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.UserEntity;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.gamegroup.module.login.controller.ChangePasswordController;
import com.sheep.gamegroup.module.login.fragments.BindAccountFgt;
import com.sheep.gamegroup.module.login.fragments.Captcha4ForgetFgt;
import com.sheep.gamegroup.module.login.fragments.NewPasswordFgt;
import com.sheep.gamegroup.module.login.fragments.NoPhoneTipFgt;
import com.sheep.gamegroup.module.login.fragments.OldPasswordFgt;
import com.sheep.gamegroup.module.login.fragments.SignInFgt;
import com.sheep.gamegroup.util.DataUtil;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.G;
import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;

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

public class ChangePasswordAct extends BaseActivity implements ChangePasswordController {

    public final static int MODE_CHANGE = 0;
    public final static int MODE_FORGET = 1;

    private String newPwd, oldPwd;

    private int mode;
    private UserEntity user;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {
        TitleBarUtils.getInstance().setShowOrHide(this, true);
        TitleBarUtils.getInstance().setTitleBack(this).setTitle(this, "设置密码");
        user = DataUtil.getInstance().getUserEntity();
        mode = getIntent().getIntExtra("mode", -1);
        if (mode == MODE_CHANGE) {
            if (user.getSet_password() == 0) {
                BindAccountFgt fgt = BindAccountFgt.newInstance(this);
                showFragment("绑定登录名密码", fgt);
            } else {
                OldPasswordFgt fgt = OldPasswordFgt.newInstance(this);
                showFragment("设置密码", fgt);
            }
        } else if (mode == MODE_FORGET) {
            if (TextUtils.isEmpty(user.getMobile())) {
                NoPhoneTipFgt fgt = NoPhoneTipFgt.newInstance(this);
                showFragment("忘记密码", fgt);
            } else {
                Captcha4ForgetFgt fgt = Captcha4ForgetFgt.newInstance(this);
                showFragment("输入验证码", fgt);
            }
        }
    }

    @Override
    public void onBackPressed() {
        if (getSupportFragmentManager().getBackStackEntryCount() == 1) {
            finish();
            return;
        }
        super.onBackPressed();
    }

    @Override
    public void whenNext(String oldPwd) {
        this.oldPwd = oldPwd;
        NewPasswordFgt fgt = NewPasswordFgt.newInstance(this);
        showFragment("设置密码", fgt);
    }

    @Override
    public void whenComplete(String newPwd) {
        this.newPwd = newPwd;
        showProgress();
        JSONObject json = new JSONObject();
        if (mode == 0) {
            json.put("old_password", oldPwd);
            json.put("new_password", newPwd);
            SheepApp.getInstance().getNetComponent().getApiService().changePassword(json)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {

                        @Override
                        public void onNext(BaseMessage baseMessage) {
                            hideProgress();
                            G.shortToast("密码修改成功");
                            finish();
                        }

                        @Override
                        public void onError(BaseMessage baseMessage) {
                            hideProgress();
                            if (baseMessage != null) {
                                G.shortToast(baseMessage);
                            } else {
                                G.shortToast("修改密码出错");
                            }
                        }
                    });
        } else {
            json.put("new_password", newPwd);
            SheepApp.getInstance().getNetComponent().getApiService().resetPass4New(json)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()){

                        @Override
                        public void onNext(BaseMessage baseMessage) {
                            hideProgress();
                            G.shortToast("密码重置成功");
                            finish();
                        }

                        @Override
                        public void onError(BaseMessage baseMessage) {
                            hideProgress();
                            if (baseMessage != null) {
                                G.shortToast(baseMessage);
                            } else {
                                G.shortToast("修改密码出错");
                            }
                        }
                    });
        }
    }

    @Override
    public void whenBindAccount() {
        G.shortToast("绑定登录名成功");
        finish();
    }

    @Override
    public void whenCaptcha(String code) {
        NewPasswordFgt fgt = NewPasswordFgt.newInstance(this);
        showFragment("重置密码", fgt);
    }

    @Override
    public void whenForget() {
        mode = MODE_FORGET;
        if (TextUtils.isEmpty(user.getMobile())) {
            NoPhoneTipFgt fgt = NoPhoneTipFgt.newInstance(this);
            showFragment("忘记密码", fgt);
        } else {
            Captcha4ForgetFgt fgt = Captcha4ForgetFgt.newInstance(this);
            showFragment("输入验证码", fgt);
        }
    }

    private void showFragment(String tag, Fragment fragment) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fragment_container, fragment, tag);
        transaction.show(fragment);
        transaction.addToBackStack(null);
        transaction.commitAllowingStateLoss();
    }

    public static void changePassword(Context context) {
        Intent intent = new Intent(context, ChangePasswordAct.class);
        intent.putExtra("mode", MODE_CHANGE);
        context.startActivity(intent);
    }

}
