package com.util;

import android.os.Handler;
import android.view.View;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TabHost;
import com.chaoran.entiry.PhotographUi;
import com.chaoran.entiry.SelfCheckBox;
import com.chaoran.entiry.SelfImage;
import com.chaoran.imp.InputInterface;
import com.chaoran.thread.ImageUrl;

import java.util.*;

/*
 * 单据工具类
 */
public class DjUtil {
	public static void setUiValue(TabHost layout, Map map, Map pageMap,
			Handler imgHandler) {
		Set<String> set = map.keySet();
		View view = null;
		String name = null;
		Object value = null;
		int fcolor = -1;
		int bcolor = -1;
		if (map.containsKey("__FCOLOR")) {
			String __fcolor = map.get("__FCOLOR").toString().trim();
			if (__fcolor.length() > 0) {
				fcolor = Integer.decode(__fcolor);
			}
		}
		if (map.containsKey("__BCOLOR")) {
			String __bcolor = map.get("__BCOLOR").toString().trim();
			if (__bcolor.length() > 0) {
				bcolor = Integer.decode(__bcolor);
			}
		}
		for (String s : set) {
			name = s.toUpperCase().trim();
			value = map.get(s).toString().trim();
			view = layout.findViewWithTag(name);
			if (view != null) {
				setText(view, value, imgHandler, fcolor, bcolor);
			} else if (pageMap.containsKey(name)) {
				if (!pageMap.get(name).equals(value)) {
					pageMap.put(name, value);
				}
			}
		}
	}

	private static void setText(View view, Object value, Handler imgHandler,
			int fcolor, int bcolor) {
		// if (view instanceof SelfEditText) {
		// System.out.println(value+"=============");
		// ((EditText) view).setText(value);
		// }else if(view instanceof CheckBox){
		// boolean b=false;
		// if(value.trim().equals("是")){
		// b=true;
		// }
		// ((CheckBox) view).setChecked(b);
		// }else if(view instanceof SelfTextBut){
		// ((SelfTextBut) view).setText(value);
		// }
		// System.out.println(view.toString()+"---"+value);
		if (view instanceof InputInterface) {
			InputInterface iiView = (InputInterface) view;
			if (fcolor > -1) {
				iiView.setTextColor(SysUtil.toColor(fcolor));
			}
			if (bcolor > -1) {
				iiView.setBackgroundDrawable(SysUtil.GradientDrawable(SysUtil
						.toColor(bcolor)));
			}
			if(value==null){
				value="";
			}
			iiView.setText(value.toString());
			if (iiView.getIsTextChange()) {
				iiView.setIsTextChange(false);
			}
		} else if (view instanceof CheckBox) {
			boolean b = false;
			if (value.equals("是")) {
				b = true;
			}
			((CheckBox) view).setChecked(b);
		} else if (view instanceof ImageView) {
			// Bitmap bmp =(Bitmap)view.getDrawingCache();
			SelfImage img = (SelfImage) view;
			if (img.getBitmap() != null) {
				img.getBitmap().recycle();
			}
			img.setBitmap(null);
			String val=value.toString();
			if (val.length() > 0) {

				new Thread(new ImageUrl(img, val, imgHandler)).start();
			}
		}else if(view instanceof PhotographUi){
			PhotographUi photo=(PhotographUi)view;
			photo.setImageBitmap(BitMapUtil.byteToBitmap((byte[])value));
		}
	}

	public static void setUiValue(TabHost layout, List list, Map pageMap,
			Handler imgHandler) {
		int len = list.size();
		Map map = null;
		String name = null;
		Object value = null;
		View view = null;
//		Object ob = null;
		int fcolor = -1;
		int bcolor = -1;
		for (int i = 0; i < len; i++) {
			map = (Map) list.get(i);
			name = map.get("fieldname").toString().trim();
			value = map.get("fieldValue");
			view = layout.findViewWithTag(name);
			if (view != null) {
				if (view instanceof InputInterface) {
					if (map.containsKey("__FCOLOR")) {
						String __fcolor = map.get("__FCOLOR").toString().trim();
						if (__fcolor.length() > 0) {
							fcolor = Integer.decode(__fcolor);
						}
					}
					if (map.containsKey("__BCOLOR")) {
						String __bcolor = map.get("__BCOLOR").toString().trim();
						if (__bcolor.length() > 0) {
							bcolor = Integer.decode(__bcolor);
						}
					}
				}
				setText(view, value, imgHandler, fcolor, bcolor);
			} else if (pageMap.containsKey(name)) {
				if (!pageMap.get(name).equals(value)) {
					pageMap.put(name, value);
				}
			}
		}
	}

	public static void setUiValue2(TabHost layout, Map map, Handler imgHandler) {
		Set<String> set = map.keySet();
		View view = null;
		ArrayList list = new ArrayList();
		for (String s : set) {
			view = layout.findViewWithTag(s);
			if (view != null) {
				// if (view instanceof SelfEditText) {
				// ((SelfEditText) view).setText(map.get(s).toString().trim());
				// } else if (view instanceof TextView) {
				// ((TextView) view).setText(map.get(s).toString().trim());
				// }
				setText(view, map.get(s).toString().trim(), imgHandler, -1, -1);
				list.add(s);
			}
		}
		for (int i = 0; i < list.size(); i++) {
			map.remove(list.get(i));
		}
	}

	public static Map organizationHzData(TabHost hzhost, String gzid) {
		FrameLayout fl = hzhost.getTabContentView();
		int count = fl.getChildCount();
		MxTabHostContent layout = null;
		// System.out.println(count + "------------------------总量");
		Map<String, String> hzData = new HashMap<String, String>();
		ArrayList pzImgList = null;
		for (int i = 0; i < count; i++) {
			layout = (MxTabHostContent) fl.getChildAt(i);
			// System.out.println(layout+"=========");
			int len = layout.getChildCount();
			View view = null;
			String value = null;
			// System.out.println(len+"---------");
			for (int j = 0; j < len; j++) {
				view = layout.getChildAt(j);
				if (view.getTag() == null) {
					continue;
				}
				if (view instanceof PhotographUi) {
					if (pzImgList == null) {
						pzImgList = new ArrayList();
					}
					PhotographUi pu = (PhotographUi) view;
					if (pu.getImageBitmap() != null) {
						HashMap imgMap = new HashMap();
						imgMap.put("photo",SysUtil.bitmapToByte(pu.getImageBitmap()));
						imgMap.put("fieldname", pu.getTag().toString());
						imgMap.put("gzid", gzid);
						pzImgList.add(imgMap);
					}
				} else {
					if (view instanceof InputInterface) {
						InputInterface et = (InputInterface) view;
						value = et.getText().toString();
						if (value == null) {
							value = "";
						}
						hzData.put(et.getTag().toString(), value.trim());
					} else if (view instanceof SelfCheckBox) {
						SelfCheckBox sfb = (SelfCheckBox) view;
						boolean b = sfb.isChecked();
						if (b) {
							hzData.put(sfb.getTag().toString(), "是");
						} else {
							hzData.put(sfb.getTag().toString(), "否");
						}
					}
				}
			}
		}
		HashMap map = new HashMap();
		map.put("hzData", hzData);
		if (pzImgList != null) {
			map.put("pzImgList", pzImgList);
		}
		return map;
	}

	/* 保存方案组织汇总数据 */
	public static Object saveOrganizationHzData(TabHost hzhost,String gzid) {
		FrameLayout fl = hzhost.getTabContentView();
		int count = fl.getChildCount();
		MxTabHostContent layout = null;
		ArrayList pzImgList = null;
		// System.out.println(count + "------------------------总量");
		Map<String, String> hzData = new HashMap<String, String>();
		for (int i = 0; i < count; i++) {
			layout = (MxTabHostContent) fl.getChildAt(i);
			int len = layout.getChildCount();
			View view = null;
			String value = null;
			for (int j = 0; j < len; j++) {
				view = layout.getChildAt(j);
				if (view.getTag() == null) {
					continue;
				}
				if (view instanceof PhotographUi) {
					PhotographUi pu = (PhotographUi) view;
					if (pu.getImageBitmap() != null) {
						
						HashMap imgMap = new HashMap();
						imgMap.put("photo",SysUtil.bitmapToByte(pu.getImageBitmap()));
						imgMap.put("fieldname", pu.getTag().toString());
						imgMap.put("gzid", gzid);
						if (pzImgList == null) {
							pzImgList = new ArrayList();
						}
						pzImgList.add(imgMap);
					}
				} else {
					if (view instanceof InputInterface) {
						InputInterface et = (InputInterface) view;
						value = et.getText().toString();
						if (et.getIsNull()) {
							if (value == null || value.length() < 1) {
								return et;
							}
						}
						if (value == null) {
							value = "";
						}
						hzData.put(et.getTag().toString(), value.trim());
					} else if (view instanceof SelfCheckBox) {
						SelfCheckBox sfb = (SelfCheckBox) view;
						boolean b = sfb.isChecked();
						if (b) {
							hzData.put(sfb.getTag().toString(), "是");
						} else {
							hzData.put(sfb.getTag().toString(), "否");
						}
					}
				}
			}
		}
		HashMap map = new HashMap();
		map.put("hzData", hzData);
		if (pzImgList != null) {
			map.put("pzImgList", pzImgList);
		}
		return map;
	}

	/* 组织明细数据插入sql */
	public static String createMxSql(ArrayList mxFieldList, String mxTempTable) {
		int len = mxFieldList.size();
		if (len < 1) {
			return null;
		}
		StringBuilder mxInsertSql = new StringBuilder();
		StringBuilder fieldName = new StringBuilder("gzid,dj_sn,dj_sort");// 列名
		StringBuilder paramName = new StringBuilder(":gzid,:dj_sn,:dj_sort");// 参数名
		String fdname = null;
		Map column = null;
		for (int i = 0; i < len; i++) {
			column = (Map) mxFieldList.get(i);
			if (!fieldName.equals("")) {
				fieldName.append(",");
				paramName.append(",");
			}
			fdname = column.get("fdname").toString().trim();
			fieldName.append(fdname);
			paramName.append(":").append(fdname);
		}
		mxInsertSql.append("insert ").append(mxTempTable).append("(")
				.append(fieldName).append(")").append("values(")
				.append(paramName).append(")");
		return mxInsertSql.toString();
	}

	/* 组织明细数据查询sql */
	public static String createMxQuerySql(ArrayList mxFieldList,
			String mxTempTable) {
		int len = mxFieldList.size();
		if (len < 1) {
			return null;
		}
		StringBuilder mxInsertSql = new StringBuilder();
		StringBuilder fieldName = new StringBuilder("gzid,dj_sn,dj_sort");// 列名
		String fdname = null;
		Map column = null;
		for (int i = 0; i < len; i++) {
			column = (Map) mxFieldList.get(i);
			if (!fieldName.equals("")) {
				fieldName.append(",");
			}
			fdname = column.get("fdname").toString().trim();
			fieldName.append(fdname);
		}
		mxInsertSql.append("select ").append(fieldName).append("  from ")
				.append(mxTempTable);
		return mxInsertSql.toString();
	}

	/* 组织明细数据 */
	public static ArrayList organizationMxData(List list, String gzid) {
		ArrayList mxData = new ArrayList();
		int len = list.size();
		Map map = null;
		for (int i = 0; i < len; i++) {
			map = (Map) list.get(i);
			map.put("gzid", gzid);
			map.put("dj_sn", i + 1);
			map.put("dj_sort", i + 1);
			mxData.add(map);
		}
		return mxData;
	}

	public static void setFocus(TabHost layout, String focus) {
		View view = layout.findViewWithTag(focus.toUpperCase().trim());
		if (view != null) {
			view.requestFocus();
		}
	}

	public static void assembleFangA(HashMap map, ArrayList list) {
		// if (list != null) {
		int len = list.size();
		HashMap tempObMap = null;
		for (int i = 0; i < len; i++) {
			tempObMap = (HashMap) list.get(i);
			map.put(tempObMap.get("funName"), tempObMap);
		}
		// }
	}

}
