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.Presenter.InputAliAccountActivityPresenter;
import com.danding.yang800.Util.StatusColorUtil;
import com.danding.yang800.Util.ToastUtil;
import com.danding.yang800.View.NetworkProgressDialog;
import com.yanzhenjie.sofia.Sofia;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

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

public class InputAliAccountActivity extends BaseActivity {
    @BindView(R.id.inputAli_saveTv)
    TextView inputAli_saveTv;
    @BindView(R.id.inputAliAccount_backTv)
    TextView inputAliAccount_backTv;
    private NetworkProgressDialog pd;
    InputAliAccountActivityPresenter presenter;
    String storeId = "";
    @BindView(R.id.inputAli_accountNameEdit)
    EditText inputAli_accountNameEdit;
    @BindView(R.id.inputAli_accountNuberEdit)
    EditText inputAli_accountNuberEdit;
    @BindView(R.id.inputAli_accountNumberAgainEdit)
    EditText inputAli_accountNumberAgainEdit;
    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_input_ali_account);
        Sofia.with(this).statusBarDarkFont().statusBarBackground(ContextCompat.getColor(this, R.color.white));
        sp = getSharedPreferences("variable", Context.MODE_PRIVATE);
        ButterKnife.bind(this);
        presenter = new InputAliAccountActivityPresenter(this);
        storeId = sp.getString("storeId", "");
        //presenter.getInfo(storeId);
    }

    @OnClick({R.id.inputAli_saveTv, R.id.inputAliAccount_backTv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.inputAli_saveTv:
                String aliName = inputAli_accountNameEdit.getText().toString().trim();
                String aliAccount = inputAli_accountNuberEdit.getText().toString().trim();
                String aliAccountAgain = inputAli_accountNumberAgainEdit.getText().toString().trim();
                if (aliName.equals("") || aliAccount.equals("") || aliAccountAgain.equals("")) {
                    ToastUtil.show(this, "账户名或账号不能为空！");
                    return;
                }
                if (!aliAccount.equals(aliAccountAgain)) {
                    ToastUtil.show(this, "账号输入不一致！");
                    return;
                }
                presenter.editInfo(storeId, getJson(aliName,
                        aliAccount, aliAccountAgain).toString());
                //startActivity(TakeToAliActivity.class);
                break;
            case R.id.inputAliAccount_backTv:
                finish();
                break;
        }

    }

    public JSONObject getJson(String name, String number, String againNumber) {
        JSONObject object = new JSONObject();
        try {
            object.put("aliName", name);
            object.put("aliAccount", number);
            object.put("aliAccountAgain", againNumber);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return object;
    }

    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 initData(String name, String number, String againNumber) {
        inputAli_accountNameEdit.setText(name);
        inputAli_accountNuberEdit.setText(number);
        inputAli_accountNumberAgainEdit.setText(againNumber);
    }

    public void editSuccess(String accountInfo) {
        Intent intent = new Intent(InputAliAccountActivity.this, TakeToAliActivity.class);
        intent.putExtra("accountInfo", accountInfo);
        startActivity(intent);
    }
}
