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

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.text.TextWatcher;
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.service.presenter.BatchLabelScannerConstact;
import cn.net.caoyao.cpzshandset.service.presenter.BatchLabelScannerConstact.Presenter;
import cn.net.caoyao.cpzshandset.service.presenter.impl.activate.kia.KIAActivateBatchLSPresenter;
import cn.net.caoyao.cpzshandset.ui.activity.base.BaseScanActivity;
import cn.net.caoyao.cpzshandset.ui.weight.MyToast;

public class BatchLabelScannerActivity extends BaseScanActivity implements BatchLabelScannerConstact.View, OnClickListener {

    private Context context;
    private MyToast myToast;
    private BatchLabelScannerConstact.Presenter presenter;
    private TextView tv_title_common;
    private Button btn_title_more_option, btn_batch_scanner_complete;
    private ViewGroup lin_batch_scanner_custom;
    private EditText et_batch_scanner_start_label, et_batch_scanner_end_label;
    private View btn_batch_scanner_start_label, btn_batch_scanner_end_label;

    @Override
    protected Handler initHandler() {
        return presenter != null ? presenter.getHandler() : null;
    }

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

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

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

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

    @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() {
        lin_batch_scanner_custom = (ViewGroup) findViewById(R.id.lin_batch_scanner_custom);
        et_batch_scanner_start_label = (EditText) findViewById(R.id.et_batch_scanner_start_label);
        btn_batch_scanner_start_label = findViewById(R.id.btn_batch_scanner_start_label);
        et_batch_scanner_end_label = (EditText) findViewById(R.id.et_batch_scanner_end_label);
        btn_batch_scanner_end_label = findViewById(R.id.btn_batch_scanner_end_label);
        btn_batch_scanner_complete = (Button) findViewById(R.id.btn_batch_scanner_complete);
    }

    @Override
    public void initPresenter() {
        switchCurrentOption(getIntent());
    }

    private void switchCurrentOption(Intent intent) {
//		int optionMode = getIntent().getIntExtra("optionMode", 0);
        HandsetOperation optionMode = (HandsetOperation) getIntent().getSerializableExtra("optionMode");

        switch (optionMode) {
            case OPERATION_ACTIVATE_CONSTANT:
                // 激活
//			createActivePresenter();
                presenter = createService.createActivateBatchLabelScannerPresenter(cacheG, context, this, intent);
                break;
            case OPERATION_PACK_CONSTANT:
                // 打包
                // TODO
                break;
            case OPERATION_OUT_WAREHOUSE_CONSTANT:
                // 出库
                // TODO
                break;
            case OPERATION_IN_WAREHOUSE_CONSTANT:
                // 入库
                // TODO
                break;
            case OPERATION_BACK_WAREHOUSE_CONSTANT:
                // 返库
                // TODO
                break;
            case OPERATION_SALE_CONSTANT:
                // 销售
                // TODO
                break;
            case OPERATION_FARM_CONSTANT:
                // 养殖
                // TODO 通用版暂时无该功能
                break;
            case OPERATION_PLANT_CONSTANT:
                // 种植
                // TODO 通用版暂时无该功能
                break;
            case OPERATION_TRAIN_SHIPMENT_CONSTANT:
                // 火车发货
                // TODO 通用版暂时无该功能
                break;
            case OPERATION_TRUCK_SHIPMENT_CONSTANT:
                // 汽车发货
                // TODO 通用版暂时无该功能
                break;
            case OPERATION_EXCHANGE_CARGO_CONSTANT:
                //羊老大 调货
                presenter = createService.createExchangeCargoBatchLabelScannerPresenter(cacheG, context, this, getIntent());
                break;
            case OPERATION_ACTIVATE_WITH_PACK_CONSTANT:
                //激活并打包
                break;
            case OPERATION_ACTIVATE_WITH_RELATION_CONSTANT:
                presenter = createService.createActivateRelationBatchLabelScannerPresenter(cacheG, context, this, intent);
                break;
            case OPERATION_PI_ACTIVATE_CONSTANT:
                presenter = new KIAActivateBatchLSPresenter(this, context, intent);
                break;
            case OPERATION_DEALER_OUT_CONSTANT:
                presenter = createService.createDealerOutBatchLabelScannerPresenter(cacheG, context, this, intent);
                break;
        }

    }

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

        btn_batch_scanner_start_label.setOnClickListener(this);
        btn_batch_scanner_end_label.setOnClickListener(this);
        btn_batch_scanner_complete.setOnClickListener(this);
    }

    @Override
    public void initData() {

    }

    @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) {
        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 finishPage() {
        finish();
    }

    @Override
    public void readQRCode() {
        PhoneScanner.Read(createService, cacheG, context);
    }

    @Override
    public void addCustomDisplayView(View customView) {
        if (lin_batch_scanner_custom == null || customView == null) return;
        lin_batch_scanner_custom.removeAllViews();
        lin_batch_scanner_custom.addView(customView);
    }

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

    @Override
    public void setStartLabel(String startLabel) {
        if (et_batch_scanner_start_label == null || startLabel == null) return;
        et_batch_scanner_start_label.setText(startLabel + "");
    }

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

    @Override
    public void setEndLabel(String endLabel) {
        if (et_batch_scanner_end_label == null || endLabel == null) return;
        et_batch_scanner_end_label.setText(endLabel + "");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.image_title_back_common:
                finish();
                break;

            case R.id.btn_title_more_option:
                if (presenter != null) {
                    presenter.uploadClick();
                }
                break;
            case R.id.btn_batch_scanner_start_label:
                if (presenter != null) {
                    presenter.btnStartLabelScannerClick();
                }
                break;
            case R.id.btn_batch_scanner_end_label:
                if (presenter != null) {
                    presenter.btnEndLabelScannerClick();
                }
                break;
            case R.id.btn_batch_scanner_complete:
                if (presenter != null) {
                    presenter.saveClick();
                }
                break;
            case R.id.et_batch_scanner_start_label:
                if (presenter != null) {
                    presenter.btnStartLabelScannerClick();
                }
                break;
            case R.id.et_batch_scanner_end_label:
                if (presenter != null) {
                    presenter.btnEndLabelScannerClick();
                }
                break;
        }
    }

    @Override
    public void setBtnSaveEnable(boolean enabled) {
        if (btn_batch_scanner_complete != null) {
            btn_batch_scanner_complete.setEnabled(enabled);
        }
    }

    @Override
    public void addStartTextChangedListener(TextWatcher t) {
        if (et_batch_scanner_start_label == null || t == null) return;
        et_batch_scanner_start_label.addTextChangedListener(t);
    }

    @Override
    public void removeStratTextChangedListener(TextWatcher t) {
        if (et_batch_scanner_start_label == null || t == null) return;
        et_batch_scanner_start_label.removeTextChangedListener(t);

    }

    @Override
    public void addEndTextChangedListener(TextWatcher t) {
        if (et_batch_scanner_end_label == null || t == null) return;
        et_batch_scanner_end_label.addTextChangedListener(t);

    }

    @Override
    public void removeEndTextChangedListener(TextWatcher t) {
        if (et_batch_scanner_end_label == null || t == null) return;
        et_batch_scanner_end_label.removeTextChangedListener(t);

    }

    @Override
    public void setBtnUploadEnable(boolean enabled) {
        if (btn_title_more_option != null) {
            btn_title_more_option.setEnabled(enabled);
        }

    }

    @Override
    public void vibrate(int duration) {
        vibrateReadQRComplete(duration);

    }

    @Override
    public void setMoreOptionVisible(int visibility) {
        if (btn_title_more_option == null) return;
        btn_title_more_option.setVisibility(visibility);

    }

}
