package com.cooleshow.usercenter.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.cooleshow.base.bean.QueryParamsConfigBean;
import com.cooleshow.base.common.BaseApplication;
import com.cooleshow.base.router.RouterPath;
import com.cooleshow.base.ui.activity.BaseMVPActivity;
import com.cooleshow.base.utils.ToastUtil;
import com.cooleshow.base.utils.ToastUtils;
import com.cooleshow.base.utils.helper.QMUIStatusBarHelper;
import com.cooleshow.usercenter.R;
import com.cooleshow.usercenter.constants.UserConstants;
import com.cooleshow.usercenter.databinding.ActivityBindPwdLayoutBinding;
import com.cooleshow.usercenter.helper.PhoneCheckHelper;
import com.cooleshow.usercenter.presenter.BindPasswordPresenter;
import com.cooleshow.usercenter.presenter.contract.BindPasswordContract;

import java.util.ArrayList;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * Author by pq, Date on 2022/4/19.
 */
@Route(path = RouterPath.UserCenter.PATH_BIND_PASSWORD)
public class BindPasswordActivity extends BaseMVPActivity<ActivityBindPwdLayoutBinding, BindPasswordPresenter> implements BindPasswordContract.BindPwdView, View.OnClickListener {
    private String tempToken;
    private OptionsPickerView pvOptions;
    private ArrayList<String> sexList = new ArrayList<>();
    private String currentGander = "";


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        QMUIStatusBarHelper.setStatusBarLightMode(this);
    }

    @NonNull
    @Override
    protected ActivityBindPwdLayoutBinding getLayoutView() {
        return ActivityBindPwdLayoutBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {
        viewBinding.tvConfirm.setOnClickListener(this);
        tempToken = getIntent().getStringExtra(UserConstants.TEMP_TOKEN);
        sexList.add("男");
        sexList.add("女");
    }

    @Override
    public void initData() {
        super.initData();
        if(BaseApplication.Companion.isTeacherClient()){
            presenter.queryModifyConfig();
            viewBinding.tvModifyConfig.setVisibility(View.VISIBLE);
        }else{
            viewBinding.tvModifyConfig.setVisibility(View.GONE);
        }
        viewBinding.tvSetSex.setOnClickListener(this);
    }

    @Override
    public void onBindSuccess() {
        ToastUtil.getInstance().showShort("设置成功");
        reBackLogin();
    }

    @Override
    public void queryModifyConfigSuccess(QueryParamsConfigBean bean) {
        if (!checkActivityExist()) {
            return;
        }
        if (bean == null) {
            return;
        }
        viewBinding.tvModifyConfig.setText(String.format("每隔%s个月可修改一次", bean.paramValue));
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tv_confirm) {
            String pwd = viewBinding.etPwd.getText().toString();
            String pwdAgain = viewBinding.etPwdAgain.getText().toString();
            String nickName = viewBinding.etNickname.getText().toString().trim();
            if (TextUtils.isEmpty(nickName)) {
                ToastUtil.getInstance().showShort("请输入昵称！");
                return;
            }
            if (TextUtils.isEmpty(currentGander)) {
                ToastUtil.getInstance().showShort("请设置性别！");
                return;
            }
            if (PhoneCheckHelper.checkPwdValidity(pwd, pwdAgain)) {
                presenter.onBindPwd(pwdAgain, nickName,currentGander);
            }
            return;
        }
        if (id == R.id.tv_set_sex) {
            selectSex();
            return;
        }
    }

    private void reBackLogin() {
        Intent intent = new Intent();
        intent.putExtra(UserConstants.TEMP_TOKEN, tempToken);
        setResult(RESULT_OK, intent);
        finish();

    }

    private void selectSex() {
        pvOptions = new OptionsPickerBuilder(this, (options1, options2, options3, v) -> {
            if (options1 < sexList.size()) {
                String s = sexList.get(options1);
                viewBinding.tvSetSex.setTextColor(getResources().getColor(com.cooleshow.base.R.color.color_333333));
                viewBinding.tvSetSex.setText(s);
                setGanderValue(s);
            }
        }).setLayoutRes(R.layout.pickerview_sex_select_layout, v -> {
            //自定义布局中的控件初始化及事件处理
            final TextView tvSubmit = (TextView) v.findViewById(com.cooleshow.base.R.id.tv_finish);
            TextView ivCancel = (TextView) v.findViewById(com.cooleshow.base.R.id.tv_cancel);
            tvSubmit.setOnClickListener(v12 -> {
                pvOptions.returnData();
                pvOptions.dismiss();
            });
            ivCancel.setOnClickListener(v1 -> pvOptions.dismiss());

        }).isDialog(false).build();
        pvOptions.setPicker(sexList);
        pvOptions.show();

    }

    @Override
    protected BindPasswordPresenter createPresenter() {
        return new BindPasswordPresenter();
    }

    private void setGanderValue(String result) {
        if (!TextUtils.isEmpty(result)) {
            currentGander = TextUtils.equals(result, "男") ? UserConstants.GENDER_TYPE_MALE : UserConstants.GENDER_TYPE_FEMALE;
        }
    }
}
