package com.huibo.mes.ui.main.activity;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.huibo.mes.R;
import com.huibo.mes.common.BaseActivity;
import com.huibo.mes.utils.BgSetUtil;
import com.huibo.mes.utils.HttpInterfaceListener;
import com.huibo.mes.utils.HttpUtil;
import com.huibo.mes.utils.SharePrfsUtil;
import com.huibo.mes.utils.ToastUtil;

import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 成品仓储单元
 */
@Deprecated
public class SystemUnit09Activity extends BaseActivity {


	private RelativeLayout unit09_tv1, unit09_tv2, unit09_tv3, unit09_tv4, unit09_tv5, unit09_tv6,
			unit09_tv7, unit09_tv8, unit09_tv9, unit09_tv10, unit09_tv11, unit09_tv12, unit09_tv13,unit09_tv14
			,unit09_tv15;
	private TextView tv_fin_row,tv_fin_col;
	List<RelativeLayout> tvList = new ArrayList<>();
	private SwipeRefreshLayout swipe_refresh_systemunit09;
	private String url;
	private static Handler mHandler = new Handler();
	private Unit09RefreshRunable refreshRunable;

	public static class Unit09RefreshRunable implements Runnable {

		WeakReference<SystemUnit09Activity> systemUnit09WeakReference;
		public Unit09RefreshRunable(SystemUnit09Activity systemUnit09Activity) {
			systemUnit09WeakReference = new WeakReference<>(systemUnit09Activity);
		}
		@Override
		public void run() {
			if (systemUnit09WeakReference.get() != null) {
				SystemUnit09Activity systemUnit09Activity = (SystemUnit09Activity) systemUnit09WeakReference.get();
				systemUnit09Activity.requestServer();
				mHandler.postDelayed(systemUnit09Activity.refreshRunable, 10 * 1000);
			}

		}
	}

	@Override
	protected void initView() {
		swipe_refresh_systemunit09 = getView(R.id.swipe_refresh_systemunit09);
		swipe_refresh_systemunit09.setColorSchemeResources(R.color.blue, R.color.red, R.color.green, R.color.purple);
		swipe_refresh_systemunit09.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
			@Override
			public void onRefresh() {
				requestServer();
			}
		});
		tv_fin_row=getView(R.id.tv_fin_row);
		tv_fin_col=getView(R.id.tv_fin_col);
		unit09_tv1 = getView(R.id.unit09_tv1);
		unit09_tv2 = getView(R.id.unit09_tv2);
		unit09_tv3 = getView(R.id.unit09_tv3);
		unit09_tv4 = getView(R.id.unit09_tv4);
		unit09_tv5 = getView(R.id.unit09_tv5);
		unit09_tv6 = getView(R.id.unit09_tv6);
		unit09_tv7 = getView(R.id.unit09_tv7);
		unit09_tv8 = getView(R.id.unit09_tv8);
		unit09_tv9 = getView(R.id.unit09_tv9);
		unit09_tv10 = getView(R.id.unit09_tv10);
		unit09_tv11 = getView(R.id.unit09_tv11);
		unit09_tv12 = getView(R.id.unit09_tv12);
		unit09_tv13 = getView(R.id.unit09_tv13);
		unit09_tv14 = getView(R.id.unit09_tv14);
		unit09_tv15 = getView(R.id.unit09_tv15);
		tvList.add(unit09_tv1);
		tvList.add(unit09_tv2);
		tvList.add(unit09_tv3);
		tvList.add(unit09_tv4);
		tvList.add(unit09_tv5);
		tvList.add(unit09_tv6);
		tvList.add(unit09_tv7);
		tvList.add(unit09_tv8);
		tvList.add(unit09_tv9);
		tvList.add(unit09_tv10);
		tvList.add(unit09_tv11);
		tvList.add(unit09_tv12);
		tvList.add(unit09_tv13);
		tvList.add(unit09_tv14);
		tvList.add(unit09_tv15);

	}

	@Override
	protected void initData() {
		refreshRunable = new Unit09RefreshRunable(this);
		mHandler.postDelayed(refreshRunable, 10 * 1000);
		url= SharePrfsUtil.getAddress(this,"FMS/equipData/GetEquipDataAction.action");
		String stateData = getIntent().getStringExtra("stateData");
		setState(stateData);
	}

	@Override
	protected String getHeaderTitle() {
		return "成品仓储单元";
	}

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

	@Override
	protected void clickCall(View view) {

	}




	private void requestServer() {
		HttpUtil.sendHttpRequest(this,url + "?stateId=9", new HttpInterfaceListener() {
			@Override
			public void onSuccess(final String str) {
				if (!str.isEmpty()) {
					runOnUiThread(new Runnable() {
						@Override
						public void run() {
							setState(str);
						}
					});
				}
			}

			@Override
			public void onFailuer(String str) {
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						ToastUtil.toastText("查询失败，请重试", 2000);
						swipe_refresh_systemunit09.setRefreshing(false);
					}
				});
			}
		});
	}

	private void setState(String stateData) {
		try {
			JSONArray jsonArray = new JSONArray(stateData);
			JSONObject jsonObject = jsonArray.getJSONObject(0);
			JSONArray array = jsonObject.getJSONArray("state");
			for(int i=0;i<8;i++){
				BgSetUtil.setData(this,tvList,array.getInt(0),i,0);
			}
			for(int i=0;i<6;i++){
				BgSetUtil.setData(this,tvList,array.getInt(1),i,8);
			}
			BgSetUtil.setSingleData(this,tvList,array.getInt(1),7,14);

			tv_fin_row.setText("出库行："+array.getInt(2));
			tv_fin_col.setText("出库列："+array.getInt(3));

		} catch (JSONException e) {

			e.printStackTrace();
		}
		swipe_refresh_systemunit09.setRefreshing(false);
		}

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