package com.danding.yang800;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.danding.yang800.General.Yang800App;
import com.danding.yang800.Presenter.CheckPhoneActivityPresenter;
import com.danding.yang800.Util.StatusColorUtil;
import com.danding.yang800.Util.ToastUtil;
import com.danding.yang800.View.AuthCodeCountDown;
import com.danding.yang800.View.NetworkProgressDialog;
import com.yanzhenjie.sofia.Sofia;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CheckPhoneActivity extends BaseActivity {
    @BindView(R.id.checkphone_saveTv)
    TextView checkphone_saveTv;
    @BindView(R.id.checkPhone_backTv)
    TextView checkPhone_backTv;
    @BindView(R.id.login_codeTv)
    TextView login_codeTv;
    @BindView(R.id.checkPhone_CodeEdit)
    EditText checkPhone_CodeEdit;
    @BindView(R.id.checkPhone_mobileTv)
    TextView checkPhone_mobileTv;
    private NetworkProgressDialog pd;
    CheckPhoneActivityPresenter presenter;
    boolean profit;
    SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        int type = StatusColorUtil.StatusBarLightMode(this);
//        if (type > 0) {
//            StatusColorUtil.setStatusBarColor(this, R.color.white);
//            StatusColorUtil.StatusBarLightMode(this, type);
//        }
        setContentView(R.layout.activity_check_phone);
        Sofia.with(this).statusBarDarkFont().statusBarBackground(ContextCompat.getColor(this, R.color.white));
        ButterKnife.bind(this);
        sp = getSharedPreferences("variable", Context.MODE_PRIVATE);
        Intent intent = getIntent();
        profit = intent.getBooleanExtra("profit", false);
        presenter = new CheckPhoneActivityPresenter(this);
        presenter.getInfo(sp.getInt("userId", 0)+"");
    }

    @OnClick({R.id.checkphone_saveTv, R.id.checkPhone_backTv, R.id.login_codeTv})
    public void onViewClicked(View view) {
        if (view.getId() != R.id.checkPhone_backTv) {
            if (!Yang800App.wifiConnect) {
                ToastUtil.show(this, "当前无网络");
                return;
            }
        }
        switch (view.getId()) {
            case R.id.checkphone_saveTv:
                presenter.messageCode(sp.getInt("userId", 0)+"", checkPhone_CodeEdit.getText().toString().trim());
                break;
            case R.id.checkPhone_backTv:
                finish();
                break;
            case R.id.login_codeTv:
                presenter.getMobileCode(sp.getInt("userId", 0)+"");
                break;
        }

    }

    public void initDialog() {
        if (pd == null) {
            pd = NetworkProgressDialog.createDialog(this);
            if (!pd.isShowing()) {
                pd.show();
            }
        }
    }

    public void dismissDialog() {
        if (pd != null) {
            pd.dismiss();
            pd = null;
            NetworkProgressDialog.clear();
        }
    }

    public void startTime() {
        AuthCodeCountDown mAuthCodeDown = new AuthCodeCountDown(60 * 1000 + 1100, login_codeTv);
        mAuthCodeDown.start();
        login_codeTv.setEnabled(false);
    }

    public void messageCodeSuccess(boolean isWithdrawCashPassword) {
        if (profit) {
            Intent intent = new Intent(CheckPhoneActivity.this, SetTakePassActivity.class);
            intent.putExtra("profit", profit);
            startActivity(intent);
        } else {
            if (isWithdrawCashPassword) {
                startActivity(ChangeTakePassActivity.class);
            } else {
                Intent intent = new Intent(CheckPhoneActivity.this, SetTakePassActivity.class);
                intent.putExtra("profit", profit);
                startActivity(intent);
            }
        }
        finish();
    }

    public void initData(String mobile) {
        checkPhone_mobileTv.setText(mobile);
    }
}
