package cn.net.caoyao.cpzshandset.ui.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import cilico.tools.barcode.PhoneScanner;
import cn.net.caoyao.cpzshandset.R;
import cn.net.caoyao.cpzshandset.common.config.HandsetOperation;
import cn.net.caoyao.cpzshandset.common.util.StringUtils;
import cn.net.caoyao.cpzshandset.service.presenter.WarehouseConfigConstact;
import cn.net.caoyao.cpzshandset.service.presenter.WarehouseConfigConstact.Presenter;
import cn.net.caoyao.cpzshandset.ui.activity.base.BaseScanActivity;
import cn.net.caoyao.cpzshandset.ui.weight.MyToast;

public class WarehouseConfigActivity extends BaseScanActivity implements OnClickListener, WarehouseConfigConstact.View {

    private Context context;
    private MyToast myToast;
    private WarehouseConfigConstact.Presenter presenter;
    private TextView tv_title_common, tv_warehouse_label;
    private Button btn_title_more_option;
    private TextView tv_warehouse_config_declare, tv_sale_config_sale_area;
    private EditText et_warehouse_config_declare;
    private ViewGroup lin_warehouse_custom_middle, lin_warehouse_config_custom_bottom;
    private View lin_sale_area, btn_sale_config_sale_area_choice;

    @Override
    public int loadView() {
        init();
        return R.layout.activity_warehouse_config_template;
    }

    private void init() {
        context = this;
        myToast = new MyToast(context);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (presenter != null) {
            presenter.onResume();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (myToast != null) {
            myToast.cancel();
        }
    }

    @Override
    public void initTitleView() {
        View image_title_back_common = findViewById(R.id.image_title_back_common);
        image_title_back_common.setVisibility(View.VISIBLE);
        image_title_back_common.setOnClickListener(this);
        tv_title_common = (TextView) findViewById(R.id.tv_title_common);
        btn_title_more_option = (Button) findViewById(R.id.btn_title_more_option);
        btn_title_more_option.setVisibility(View.VISIBLE);
        btn_title_more_option.setOnClickListener(this);
    }

    @Override
    public void initView() {
        tv_warehouse_config_declare = (TextView) findViewById(R.id.tv_warehouse_config_declare);
        et_warehouse_config_declare = (EditText) findViewById(R.id.et_warehouse_config_declare);
        lin_sale_area = findViewById(R.id.lin_sale_area);
        tv_sale_config_sale_area = (TextView) findViewById(R.id.tv_sale_config_sale_area);
        btn_sale_config_sale_area_choice = findViewById(R.id.btn_sale_config_sale_area_choice);
        lin_warehouse_config_custom_bottom = (ViewGroup) findViewById(R.id.lin_warehouse_config_custom_bottom);
        lin_warehouse_custom_middle = (ViewGroup) findViewById(R.id.lin_warehouse_custom_middle);
        tv_warehouse_label = (TextView) findViewById(R.id.tv_warehouse_label);
    }

    @Override
    public void initPresenter() {
        switchWarehouseOption();
    }

    private void switchWarehouseOption() {
        Intent intent = getIntent();
//		int intent.getIntExtra("optionMode", 0);
        HandsetOperation optionMode = (HandsetOperation) intent.getSerializableExtra("optionMode");
        switch (optionMode) {
            case OPERATION_OUT_WAREHOUSE_CONSTANT:
                // 出库
                presenter = createService.createOutWarehouseConfigPresenter(cacheG, context, this, intent);
                break;
            case OPERATION_IN_WAREHOUSE_CONSTANT:
                // 入库
                presenter = createService.createInWarehouseConfigPresenter(cacheG, context, this, intent);
                break;
            case OPERATION_BACK_WAREHOUSE_CONSTANT:
                // 返库
                presenter = createService.createBackWarehouseConfigPresenter(cacheG, context, this, intent);
                break;
            case OPERATION_DEALER_OUT_CONSTANT:
                presenter = createService.createDealerOutConfigPresenter(cacheG, context, this, intent);
                break;
        }
    }

    @Override
    public void setListener() {
        btn_sale_config_sale_area_choice.setOnClickListener(this);

    }

    @Override
    public void initData() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_sale_config_sale_area_choice:
                if (presenter != null) {
                    presenter.choiceSaleAreaClick();
                }
                break;
            case R.id.image_title_back_common:
                finish();
                break;
            case R.id.btn_title_more_option:
                if (presenter != null) {
                    presenter.moreOptionClick();
                }
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (presenter != null) {
            presenter.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    public void setPresenter(Presenter presenter) {
        if (presenter != null) {
            this.presenter = presenter;
        }
    }

    @Override
    public void startAcForResult(int reqCode, Intent intent) {
        if (intent != null) {
            startActivityForResult(intent, reqCode);
        }

    }

    @Override
    public void showMsgToast(String msg, int duration) {
        if (myToast == null || StringUtils.isEmpty(msg)) return;
        myToast.show(msg, duration);
    }

    @Override
    public void setPageTitle(String homeTitle) {
        if (tv_title_common == null || homeTitle == null) return;
        tv_title_common.setText(homeTitle + "");
    }

    @Override
    public void setMoreOptionStr(String moreStr) {
        if (btn_title_more_option == null || moreStr == null) return;
        btn_title_more_option.setText(moreStr + "");

    }

    @Override
    public void setTVWarehousDeclareStr(String declareStr) {
        if (tv_warehouse_config_declare == null || declareStr == null) return;
        tv_warehouse_config_declare.setText(declareStr + "");
    }

    @Override
    public String getEtWarehousDeclareStr() {
        return et_warehouse_config_declare == null ? "" : et_warehouse_config_declare.getText().toString().trim();
    }

    @Override
    public void setEtWarehousDeclareStr(String describeStr) {
        if (et_warehouse_config_declare != null && describeStr != null) {
            et_warehouse_config_declare.setText(describeStr + "");
        }
    }

    @Override
    public void addBottomCustomView(View bottomCustomView) {
        if (lin_warehouse_config_custom_bottom == null || bottomCustomView == null) return;
        lin_warehouse_config_custom_bottom.removeAllViews();
        lin_warehouse_config_custom_bottom.addView(bottomCustomView);
    }

    @Override
    public void finishPage() {
        finish();
    }

    @Override
    public void setSaleAreaVisiable(int visiable) {
        if (lin_sale_area != null) {
            lin_sale_area.setVisibility(visiable);
        }

    }

    @Override
    public void setSaleAreaStr(String salesArea) {
        if (tv_sale_config_sale_area != null && salesArea != null) {
            tv_sale_config_sale_area.setText(salesArea + "");
        }

    }

    @Override
    public void addMiddleCustomView(View middleCustomView) {
        if (lin_warehouse_custom_middle == null || middleCustomView == null) return;
        lin_warehouse_custom_middle.removeAllViews();
        lin_warehouse_custom_middle.addView(middleCustomView);
    }

    @Override
    public void setMoreOptionVisiable(int visiable) {
        if (btn_title_more_option == null) return;
        btn_title_more_option.setVisibility(visiable);
    }

    @Override
    public void setWarehouseLabelStr(String labelStr) {
        if (tv_warehouse_label == null || labelStr == null) return;
        tv_warehouse_label.setText(labelStr + "");

    }

    @SuppressLint("HandlerLeak")
    @Override
    protected Handler initHandler() {
        return new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case PhoneScanner.BARCODE_ERR_LABEL:
                        et_warehouse_config_declare.setText(msg.obj.toString());
                    case PhoneScanner.BARCODE_READ:
                        et_warehouse_config_declare.setText(msg.obj.toString());
                        break;
                    case PhoneScanner.BARCODE_NOREAD:
                        et_warehouse_config_declare.setText(msg.obj.toString());
                        break;
                    default:
                        et_warehouse_config_declare.setText(msg.obj.toString());
                        break;
                }
            }
        };
    }
}
