package com.taikor.investment.modules.user;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.taikor.investment.R;
import com.taikor.investment.base.BaseActivity;
import com.taikor.investment.utils.CountDownTimerUtils;
import com.taikor.investment.utils.SharedPreferenceUtils;
import com.taikor.investment.utils.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 手机绑定
 * Created by Any on 2017/4/12.
 */
public class PhoneBindingActivity extends BaseActivity {

    @BindView(R.id.tv_bar_title)
    TextView tvBarTitle;//标题
    @BindView(R.id.et_bind_phone)
    EditText etBindPhone;//手机号
    @BindView(R.id.iv_delete)
    ImageView ivDelete;//删除
    @BindView(R.id.et_bind_code)
    EditText etBindCode;//验证码
    @BindView(R.id.tv_confirm_number)
    TextView tvConfirmNumber;//获取验证码
    @BindView(R.id.bt_sure)
    Button btSure;//确认
    @BindView(R.id.ll_info)
    LinearLayout llInfo;//信息提示
    @BindView(R.id.tv_current_phone)
    TextView tvCurrentPhone;//当前手机号码

    private String phone, code;
    private Intent intent;

    @Override
    public int getContentViewId() {
        return R.layout.activity_user_phone_bind;
    }

    @Override
    public void initView() {
        btSure.getBackground().setAlpha(125);
        etBindPhone.addTextChangedListener(new BindWatcher("phone"));
        etBindCode.addTextChangedListener(new BindWatcher("code"));

        intent = getIntent();
        boolean bind = intent.getBooleanExtra("bind", false);
        if (bind) {
            //绑定
            tvBarTitle.setText(getString(R.string.phone_bind));
            btSure.setText("绑定手机");
        } else {
            String bindPhone = intent.getStringExtra("phone");
            //修改
            tvBarTitle.setText("换绑手机");
            btSure.setText("确认换绑");
            llInfo.setVisibility(View.VISIBLE);
            phone = SharedPreferenceUtils.getString(this, "phone", null);
            tvCurrentPhone.setText("当前手机号：" + hidePhoneNumber(bindPhone));
        }
    }

    //手机号，验证码输入监听
    private class BindWatcher implements TextWatcher {
        private String type;

        BindWatcher(String type) {
            this.type = type;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (type.equals("phone")) {
                phone = s.toString();
            } else if (type.equals("code")) {
                code = s.toString();
                btSure.getBackground().setAlpha(255);
            }
        }
    }

    //隐藏手机号码
    public String hidePhoneNumber(String num) {
        if (!TextUtils.isEmpty(num) && num.length() > 6) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < num.length(); i++) {
                char c = num.charAt(i);
                if (i >= 3 && i <= 6) {
                    sb.append('*');
                } else {
                    sb.append(c);
                }
            }
            return sb.toString();
        }
        return null;
    }

    @OnClick({R.id.tv_bar_back, R.id.iv_delete, R.id.tv_confirm_number, R.id.bt_sure})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_bar_back:
                finish();
                break;
            case R.id.iv_delete:
                etBindPhone.setText("");
                break;
            case R.id.tv_confirm_number://发送验证码
                //开始倒计时
                CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(tvConfirmNumber, 30000, 1000);
                mCountDownTimerUtils.start();
                break;
            case R.id.bt_sure://确认
                //如果用户填写了手机号,验证码
                if (TextUtils.isEmpty(phone) || TextUtils.isEmpty(code)) {
                    ToastUtils.showShort(PhoneBindingActivity.this, "没有填写完整！");
                } else {
                    SharedPreferenceUtils.saveString(PhoneBindingActivity.this, "phone", phone);
                    intent.putExtra("phone", phone);//传递手机号码
                    setResult(RESULT_OK, intent);
                    finish();
                }
                break;
        }
    }
}

