package com.sheep.gamegroup.view.activity;

import android.app.Activity;
import android.support.v7.widget.AppCompatEditText;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.sheep.gamegroup.di.components.DaggerBindDataComponent;
import com.sheep.gamegroup.di.modules.BindDataModule;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.presenter.BindDataContract;
import com.sheep.gamegroup.presenter.BindDataPresenter;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.gamegroup.absBase.BaseActivity;
import com.sheep.jiuyan.samllsheep.utils.G;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 绑定数据
 * Created by ljy on 2018/3/19.
 */

public class BindDataAct extends BaseActivity implements BindDataContract.View {
    @BindView(R.id.text_notice_tv)
    TextView textNoticeTv;
    @BindView(R.id.text_et)
    AppCompatEditText textEt;
    @BindView(R.id.text_layout)
    LinearLayout textLayout;
    @BindView(R.id.number_notice_tv)
    TextView numberNoticeTv;
    @BindView(R.id.number_et)
    AppCompatEditText numberEt;
    @BindView(R.id.number_layout)
    LinearLayout numberLayout;
    @BindView(R.id.sure_text)
    TextView sureText;
    @Inject
    BindDataPresenter presenter;
    @BindView(R.id.img_baseactivity_title)
    ImageView imgBaseactivityTitle;
    @BindView(R.id.txt_baseactivity_title)
    TextView txtBaseactivityTitle;
    private Activity activity;

    private int type = 0;
    private String key = "real_name";
    private String value = "";
    private String title = "绑定真实姓名";

    @Override
    public void upView(Object o) {
        hideProgress();
        G.showToast("绑定成功");
        finish();

    }

    @Override
    public void failView(Object o) {
        G.showToast(((BaseMessage)o).getMsg()+"");
        hideProgress();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.bind_data_act_layout;
    }

    @Override
    public void initView() {
        activity = this;
        DaggerBindDataComponent.builder()
                .netComponent(SheepApp.get(this).getNetComponent())
                .bindDataModule(new BindDataModule(this))
                .build()
                .inject(this);
        type = getIntent().getIntExtra("stype", 0);
        assemblyData(type);
        txtBaseactivityTitle.setText(title);
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {
    }

    @OnClick({R.id.sure_text, R.id.img_baseactivity_title})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.sure_text:
                assemblyData(type);
                submit();
                break;
            case R.id.img_baseactivity_title:
                finish();
                break;
        }
    }

    private void submit() {
        if(android.text.TextUtils.isEmpty(value)){
            G.showToast("请填入数据");
            return;
        }
        JSONObject j = new JSONObject();
        j.put(key, value);
        showProgress();
        switch (type) {
            case 40001:
                presenter.submitDataRealname(j);
                break;
            case 40002:
                presenter.submitDataIdnumber(j);
                break;
            case 40003:
                presenter.submitDataBankcard(j);
                break;
        }
    }

    private String assemblyData(int type){
        switch (type) {
            case 40001:
                key = "real_name";
                title = "绑定真实姓名";
                numberLayout.setVisibility(View.GONE);
                textNoticeTv.setText("真实姓名");
                value = textEt.getText().toString();
                break;
            case 40002:
                key = "id_number";
                title = "绑定身份证";
                numberLayout.setVisibility(View.GONE);
                textNoticeTv.setVisibility(View.GONE);
                value = textEt.getText().toString();
                break;
            case 40003:
                key = "bank_card";
                title = "绑定银行卡";
                textLayout.setVisibility(View.GONE);
                numberNoticeTv.setText("卡号");
                value = numberEt.getText().toString();
                break;
        }
        return value;
    }
}
