package cn.net.caoyao.cpzshandset.service.presenter.impl.upload.jinLiquor;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import cn.net.caoyao.cpzshandset.R;
import cn.net.caoyao.cpzshandset.bean.JinLiquorUploadBean;
import cn.net.caoyao.cpzshandset.common.config.HandsetOperation;
import cn.net.caoyao.cpzshandset.service.presenter.UploadStatusConstact;
import cn.net.caoyao.cpzshandset.service.presenter.base.impl.UploadStatusConstactImpl;
import cn.net.caoyao.cpzshandset.ui.adapter.JinLiquorUploadStatusAdapter;
import cn.net.caoyao.cpzshandset.ui.adapter.ListTextAdapter.IOnItemSelectListener;
import cn.net.caoyao.cpzshandset.ui.adapter.OptionModeAdapter;
import cn.net.caoyao.cpzshandset.ui.adapter.SpinnerOptionModePopWindow;

public class JinLiquorUploadStatus extends UploadStatusConstactImpl {
	private final UploadStatusConstact.View uView;
	private Context context;
	private List<HandsetOperation> options = new ArrayList<>();
	private HandsetOperation currentOption;
	private SpinnerOptionModePopWindow optionModeSpinnerPopWindow;
	private View rel_option_mode;
	private TextView tv_option_mode;
	private List<JinLiquorUploadBean> uploads = new ArrayList<>();
	private JinLiquorUploadStatusAdapter adapter;
	public JinLiquorUploadStatus(UploadStatusConstact.View uView,Context context) {
		this.uView = uView;
		this.context = context;
		start();
	}

	@Override
	public void start() {
		uView.setPageTitle("本机记录");
		initOptions();
		
		View customView = LayoutInflater.from(context).inflate(R.layout.layout_upload_status_custom, null);
		uView.addCustomView(customView);
		initCustomView(customView);
		
		initOptionModeSpinnerView();
		
		adapter = new JinLiquorUploadStatusAdapter(uploads);
		uView.setAdapter(adapter);
		addTestData();
	}

	private void addTestData() {
		for (int i = 0; i < 10; i++) {
			JinLiquorUploadBean b = new JinLiquorUploadBean();
			b.setOutWarehouseNo("123456879524154" + i);
			String date = "2017-10-1" + i +" 14:13:5" + i;
			b.setUpload_date(date);
			b.setOption_type(HandsetOperation.OPERATION_OUT_WAREHOUSE_CONSTANT.getOption());
			b.setTray_count(300 + (i * 45));
			b.setLabel_count(i + (i * 6));
			if (i%2 == 0) {
				b.setDealer_name("吉林 " + i +" 级经销商");
			} else {
				b.setDealer_name("黑龙江" + i +" 级经销商");
			}
			uploads.add(b);
		}
		adapter.refreshData(uploads);
		
	}

	private void initCustomView(View customView) {
		rel_option_mode = customView.findViewById(R.id.rel_option_mode);
		tv_option_mode = (TextView) customView.findViewById(R.id.tv_option_mode);
		tv_option_mode.setText(currentOption.getOptionName());
		rel_option_mode.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				showOptionModeSpinnerWindow();
				
			}
		});
	}

	/**
	 * 初始化可选手持机的信息
	 */
	private void initOptionModeSpinnerView() {
		OptionModeAdapter mAdapter = new OptionModeAdapter(options);
      //初始化PopWindow
		optionModeSpinnerPopWindow = new SpinnerOptionModePopWindow(context);
		optionModeSpinnerPopWindow.setAdatper(mAdapter);
		optionModeSpinnerPopWindow.setItemListener(new IOnItemSelectListener() {
			@Override
			public void onItemClick(int pos) {
				currentOption = options.get(pos);
				tv_option_mode.setText(currentOption.getOptionName());
			}
		});		
	
	}
	private void showOptionModeSpinnerWindow(){
		optionModeSpinnerPopWindow.setWidth(rel_option_mode.getWidth());
		optionModeSpinnerPopWindow.showAsDropDown(tv_option_mode);
    }
	private void initOptions() {
		currentOption = HandsetOperation.OPERATION_OUT_WAREHOUSE_CONSTANT;
		options.add(HandsetOperation.OPERATION_OUT_WAREHOUSE_CONSTANT);
		options.add(HandsetOperation.OPERATION_RETURNED_GOODS_CONSTANT);
		options.add(HandsetOperation.OPERATION_REPLENISH_BING_CONSTANT);
		options.add(HandsetOperation.OPERATION_ORGANIZATION_BING_CONSTANT);
		
	}

	@Override
	public boolean checkViewAvailable() {
		boolean b = false;
        if (uView != null && context != null) {
            b = true;
        }
        return b;
	}

	
	
	
}
