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.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.danding.yang800.Presenter.TakeToAliActivityPresenter;
import com.danding.yang800.Util.StatusColorUtil;
import com.danding.yang800.View.NetworkProgressDialog;
import com.yanzhenjie.sofia.Sofia;

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

public class TakeToAliActivity extends BaseActivity {
    @BindView(R.id.taketoAli_backTv)
    TextView taketoAli_backTv;
    @BindView(R.id.takeToAli_fogetPassTv)
    TextView takeToAli_fogetPassTv;
    @BindView(R.id.takeToAli_applyTv)
    TextView takeToAli_applyTv;
    @BindView(R.id.takeToAli_moneyEdit)
    EditText takeToAli_moneyEdit;
    @BindView(R.id.takeToAli_passEdit)
    EditText takeToAli_passEdit;
    String accountInfo;
    private NetworkProgressDialog pd;
    TakeToAliActivityPresenter presenter;
    String storeId = "";
    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_take_to_ali);
        Sofia.with(this).statusBarDarkFont().statusBarBackground(ContextCompat.getColor(this, R.color.white));
        sp = getSharedPreferences("variable", Context.MODE_PRIVATE);
        ButterKnife.bind(this);
        storeId=sp.getString("storeId", "");
        Intent intent = getIntent();
        accountInfo = intent.getStringExtra("accountInfo");
        presenter = new TakeToAliActivityPresenter(this);
        takeToAli_moneyEdit.requestFocus();
        takeToAli_moneyEdit.setHint("当前可提现最大金额" + sp.getString("money", "") + "元");
        takeToAli_moneyEdit.addTextChangedListener(new TextWatcher() {
            public void afterTextChanged(Editable edt) {

            }

            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

            }

            public void onTextChanged(CharSequence s, int arg1, int arg2, int arg3) {
                if (s.toString().contains(".")) {
                    if (s.length() - 1 - s.toString().indexOf(".") > 2) {
                        s = s.toString().subSequence(0,
                                s.toString().indexOf(".") + 3);
                        takeToAli_moneyEdit.setText(s);
                        takeToAli_moneyEdit.setSelection(s.length());
                    }
                }
                if (s.toString().trim().substring(0).equals(".")) {
                    s = "0" + s;
                    takeToAli_moneyEdit.setText(s);
                    takeToAli_moneyEdit.setSelection(2);
                }

                if (s.toString().startsWith("0")
                        && s.toString().trim().length() > 1) {
                    if (!s.toString().substring(1, 2).equals(".")) {
                        takeToAli_moneyEdit.setText(s.subSequence(0, 1));
                        takeToAli_moneyEdit.setSelection(1);
                        return;
                    }
                }
            }
        });

    }

    @OnClick({R.id.taketoAli_backTv, R.id.takeToAli_fogetPassTv, R.id.takeToAli_applyTv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.taketoAli_backTv:
                finish();
                break;
            case R.id.takeToAli_fogetPassTv:
                Intent intent = new Intent(TakeToAliActivity.this, CheckPhoneActivity.class);
                intent.putExtra("profit", true);
                startActivity(intent);
                break;
            case R.id.takeToAli_applyTv:
                presenter.apply(storeId, "", takeToAli_passEdit.getText().toString().trim(),
                        takeToAli_moneyEdit.getText().toString().trim(), accountInfo);
                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 applySuccess() {
        startActivity(MyProfitActivity.class);
    }
}
