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.ViewGroup;
import android.view.View.OnClickListener;
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.util.StringUtils;
import cn.net.caoyao.cpzshandset.service.presenter.SaleConfigConstact;
import cn.net.caoyao.cpzshandset.service.presenter.SaleConfigConstact.Presenter;
import cn.net.caoyao.cpzshandset.ui.activity.base.BaseScanActivity;
import cn.net.caoyao.cpzshandset.ui.weight.MyToast;

public class SaleConfigActivity extends BaseScanActivity implements SaleConfigConstact.View, OnClickListener{
	private SaleConfigConstact.Presenter presenter;
	private Context context;
	private MyToast myToast;
	private TextView tv_title_common,tv_sale_config_sale_area,tv_sale_area_label;
	private Button btn_title_more_option;
	private View btn_sale_config_sale_area_choice;
	private ViewGroup lin_sale_config_custom;
	private EditText et_sale_config_declare;
	
	@Override
	public int loadView() {
		init();
		return R.layout.activity_sale_config_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() {
		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_sale_config_custom = (ViewGroup) findViewById(R.id.lin_sale_config_custom);
		et_sale_config_declare = (EditText) findViewById(R.id.et_sale_config_declare);
		tv_sale_area_label = (TextView) findViewById(R.id.tv_sale_area_label);
	}

	@Override
	public void initPresenter() {
		presenter = createService.createSaleConfigPresenter(cacheG, context, this, getIntent());
	}

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

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

	@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
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(presenter == null) return;
		presenter.onActivityResult(requestCode, resultCode, data);
	}
	@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 String getEtSaleDeclareStr() {
		return et_sale_config_declare == null ? "" : et_sale_config_declare.getText().toString().trim();
	}

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

	@Override
	public void setSaleArea(String saleArea) {
		if(tv_sale_config_sale_area == null || saleArea == null) return;
		tv_sale_config_sale_area.setText(saleArea +"");
	}

	@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) return;
			presenter.moreOptionClick();
			break;
		case R.id.btn_sale_config_sale_area_choice:
			if (presenter == null) return;
			presenter.choiceSaleAddressClick();
			break;
		}
	}

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

	@Override
	public void setEtSaleDeclareStr(String describe) {
		if (et_sale_config_declare != null && describe != null) {
			et_sale_config_declare.setText(describe+"");
		}
		
	}

	@Override
	public void setSaleLabelStr(String saleLabelStr) {
		if(tv_sale_area_label == null || saleLabelStr == null) return;
		tv_sale_area_label.setText(saleLabelStr+"");
		
	}
	@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_sale_config_declare.setText(msg.obj.toString());
					case PhoneScanner.BARCODE_READ:
						et_sale_config_declare.setText(msg.obj.toString());
						break;
					case PhoneScanner.BARCODE_NOREAD:
						et_sale_config_declare.setText(msg.obj.toString());
						break;
					default:
						et_sale_config_declare.setText(msg.obj.toString());
						break;
				}
			}
		};
	}
}
