package cn.net.connor.std.processedit.newmeprocess;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;



import com.teamcenter.rac.kernel.ListOfValuesInfo;
import com.teamcenter.rac.kernel.TCComponentListOfValues;
import com.teamcenter.rac.kernel.TCComponentListOfValuesType;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.kernel.TCProperty;
import com.teamcenter.rac.kernel.TCPropertyDescriptor;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.soa.client.model.LovValue;

public class LovUtil {


    public static void setTCArrProperty(TCProperty property, String[] value) {
        try {
            if (value == null) {
                return;
            }
            TCPropertyDescriptor descriptor = property.getPropertyDescriptor();
            boolean isArray = descriptor.isArray();
            if (isArray) {
                property.setStringValueArray(value);
            }
        } catch (Exception e) {
            System.out.println("赋值异常："+e);
        }
    }

    /**
     * 
     * @param property
     * @param value
     * @throws TCException
     */
    public static void setTCProperty(TCProperty property, String value) {
        try {
            TCPropertyDescriptor descriptor = property.getPropertyDescriptor();
            boolean isArray = descriptor.isArray();
            if (!isArray) {
                switch (descriptor.getType()) {

                case TCProperty.PROP_string:
                case TCProperty.PROP_long_string:
                    property.setStringValueData(value);
                    break;
                case TCProperty.PROP_char:
                    if (value.length() > 0) {
                        property.setCharValueData(value.charAt(0));
                    }
                    break;
                case TCProperty.PROP_double:
                    property.setDoubleValueData(Double.parseDouble(value));
                    break;
                case TCProperty.PROP_float:
                    property.setFloatValueData(Float.parseFloat(value));
                    break;
                case TCProperty.PROP_int:
                case TCProperty.PROP_short:
                    property.setIntValueData(Integer.parseInt(value));
                    break;
                case TCProperty.PROP_logical:
                    property.setLogicalValueData(Boolean.parseBoolean(value));
                    break;
                case TCProperty.PROP_date:
                    try {
                        if (!value.isEmpty()) {
                            property.setDateValueData(new SimpleDateFormat("yyyy/MM/dd").parse(value));
                        }
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    break;
                }
            } else {
                if (value.indexOf(",") > 0) {
                    property.setStringValueArray(value.split(","));
                } else {
                    property.setStringValueArray(new String[] { value });
                }
                // System.out.println("对应的属性为数组类型，不予以处理");
            }
        } catch (Exception e) {
            System.out.println("赋值异常："+ e);
        }

    }

    public static Map<String, String[]> getLovThree(TCSession session, String lovName) {
        Map<String, String[]> notSaveAsMap = new HashMap<String, String[]>();
        Map<String, String[]> SaveAsMap = new HashMap<String, String[]>();
        notSaveAsMap.clear();
        SaveAsMap.clear();
        try {
            TCComponentListOfValues lov = TCComponentListOfValuesType.findLOVByName(session, lovName);
            if (lov != null) {
                ListOfValuesInfo lovInfo = lov.getListOfValues();
                System.out.println("lovInfo:" + lovInfo);
                String[] size = lovInfo.getLOVDisplayValues();
                for (int i = 0; i < size.length; i++) {
                    TCComponentListOfValues Component = lovInfo.getListOfFilters()[i];
                    ListOfValuesInfo child = Component.getListOfValues();
                    String[] arr = child.getLOVDisplayValues();
                    System.out.println("size:" + size[i]);
                    System.out.println("Component:" + Component);
                    System.out.println("arr:" + arr.length);
                    notSaveAsMap.put(size[i], arr);
                    for (int j = 0; j < arr.length; j++) {
//						if(!"H8_ckxd_LOV".equals(child.toString())) {
//							continue;
//						}
                        TCComponentListOfValues children = child.getListOfFilters()[j];
                        ListOfValuesInfo childs = children.getListOfValues();
                        String[] number = childs.getLOVDisplayValues();
                        SaveAsMap.put(arr[j], number);
                    }
                }
                return SaveAsMap;
            }
        } catch (TCException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Map<String, String> getLovThreeRealValue(TCSession session, String lovName) {
        Map<String, String> SaveAsMap = new HashMap<String, String>();
        SaveAsMap.clear();
        try {
            TCComponentListOfValues lov = TCComponentListOfValuesType.findLOVByName(session, lovName);
            if (lov != null) {
                ListOfValuesInfo lovInfo = lov.getListOfValues();
                String[] size = lovInfo.getLOVDisplayValues();
                for (int i = 0; i < size.length; i++) {
                    TCComponentListOfValues Component = lovInfo.getListOfFilters()[i];
                    ListOfValuesInfo child = Component.getListOfValues();
                    String[] arr = child.getLOVDisplayValues();
                    for (int j = 0; j < arr.length; j++) {

                        TCComponentListOfValues children = child.getListOfFilters()[j];
                        ListOfValuesInfo childs = children.getListOfValues();
                        String[] number = childs.getLOVDisplayValues();
                        List<LovValue> values = childs.getValues();
                        for (int k = 0; k < number.length; k++) {
                            SaveAsMap.put(number[k], values.get(k).getStringValue());
                        }
                    }
                }
                return SaveAsMap;
            }
        } catch (TCException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Map<String, String[]> getLovTWO(TCSession session, String lovName) {
        Map<String, String[]> notSaveAsMap = new HashMap<String, String[]>();
        Map<String, String[]> SaveAsMap = new HashMap<String, String[]>();
        notSaveAsMap.clear();
        SaveAsMap.clear();
        try {
            TCComponentListOfValues lov = TCComponentListOfValuesType.findLOVByName(session, lovName);
            if (lov != null) {
                ListOfValuesInfo lovInfo = lov.getListOfValues();
                System.out.println("lovInfo:" + lovInfo);
                String[] size = lovInfo.getLOVDisplayValues();
                for (int i = 0; i < size.length; i++) {
                    TCComponentListOfValues Component = lovInfo.getListOfFilters()[i];
                    ListOfValuesInfo child = Component.getListOfValues();
                    String[] arr = child.getLOVDisplayValues();
                    System.out.println("size:" + size[i]);
                    System.out.println("Component:" + Component);
                    System.out.println("arr:" + arr.length);
                    notSaveAsMap.put(size[i], arr);
                }
                return notSaveAsMap;
            }
        } catch (TCException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Map<String, String> getPropertyone(TCSession session, String lovName) {
        Map<String, String> notSaveAsMap = new HashMap<String, String>();
        notSaveAsMap.clear();
        try {
            TCComponentListOfValues lov = TCComponentListOfValuesType.findLOVByName(session, lovName);
            if (lov != null) {
                ListOfValuesInfo lovInfo = lov.getListOfValues();
                System.out.println("lovInfo:" + lovInfo);
                String[] size = lovInfo.getStringListOfValues();
                String[] arr = lovInfo.getLOVDisplayValues();
                for (int i = 0; i < size.length; i++) {
                    System.out.println("size:" + size[i]);
                    notSaveAsMap.put(arr[i], size[i]);
                }
                return notSaveAsMap;
            }
        } catch (TCException e) {
            e.printStackTrace();
        }

        return null;
    }

    public static Map<String, String> getLov(TCSession session, String lovName) {
        Map<String, String> SaveAsMap = new LinkedHashMap<String, String>();
        SaveAsMap.clear();
        try {
            TCComponentListOfValues lov = TCComponentListOfValuesType.findLOVByName(session, lovName);
            if (lov != null) {
                ListOfValuesInfo lovInfo = lov.getListOfValues();

                System.out.println("lovInfo:" + lovInfo);
                String[] size = lovInfo.getLOVDisplayValues();
                for (int i = 0; i < size.length; i++) {
                    TCComponentListOfValues Component = lovInfo.getListOfFilters()[i];
                    ListOfValuesInfo child = Component.getListOfValues();
                    System.out.println("size:" + size[i]);
                    System.out.println("Component:" + Component);
                    SaveAsMap.put(size[i], Component.toString());
                }
                return SaveAsMap;

            }
        } catch (TCException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Map<String, String> getPropertytwo(TCSession session, String lovName) {
        Map<String, String> SaveAsMap = new HashMap<String, String>();
        SaveAsMap.clear();
        try {
            TCComponentListOfValues lov = TCComponentListOfValuesType.findLOVByName(session, lovName);
            if (lov != null) {
                ListOfValuesInfo lovInfo = lov.getListOfValues();
                System.out.println("lovInfo:" + lovInfo);
                String[] size = lovInfo.getLOVDisplayValues();
                for (int i = 0; i < size.length; i++) {
                    TCComponentListOfValues Component = lovInfo.getListOfFilters()[i];
                    ListOfValuesInfo child = Component.getListOfValues();
                    String[] arr = child.getStringListOfValues();
                    String[] arrs = child.getLOVDisplayValues();
                    System.out.println("size:" + size[i]);
                    System.out.println("Component:" + Component);
                    System.out.println("child:" + child);
                    for (int j = 0; j < arr.length; j++) {
                        System.out.println("arr:" + arr[j]);
                        System.out.println("arrs:" + arrs[j]);
                        SaveAsMap.put(arrs[j], arr[j]);
                    }
                }
                System.out.println(SaveAsMap.toString());
                return SaveAsMap;

            }
        } catch (TCException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Map<String, String> getPropertythree(TCSession session, String lovName) {
        Map<String, String> SaveAsMap = new HashMap<String, String>();
        SaveAsMap.clear();
        try {
            TCComponentListOfValues lov = TCComponentListOfValuesType.findLOVByName(session, lovName);
            if (lov != null) {
                ListOfValuesInfo lovInfo = lov.getListOfValues();
                System.out.println("lovInfo:" + lovInfo);
                String[] size = lovInfo.getLOVDisplayValues();
                for (int i = 0; i < size.length; i++) {
                    TCComponentListOfValues Component = lovInfo.getListOfFilters()[i];
                    ListOfValuesInfo child = Component.getListOfValues();
                    String[] arr = child.getStringListOfValues();
                    System.out.println("size:" + size[i]);
                    System.out.println("Component:" + Component);
                    System.out.println("arr:" + arr.length);
                    for (int j = 0; j < arr.length; j++) {
                        TCComponentListOfValues children = Component.getListOfFilters()[j];
                        ListOfValuesInfo childs = children.getListOfValues();
                        String[] number = childs.getStringListOfValues();
                        String[] numbers = childs.getLOVDisplayValues();
                        for (int k = 0; k < number.length; k++) {
                            SaveAsMap.put(numbers[k], number[k]);
                        }

                    }
                }
                return SaveAsMap;
            }
        } catch (TCException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 取单层LOV的真实值
     * 
     * @param session
     * @param lovName
     * @return
     */
    public static String getLovRealValue(TCSession session, String lovName, String displayValue) {
        try {
            TCComponentListOfValues lov = TCComponentListOfValuesType.findLOVByName(session, lovName);
            if (lov != null) {
                ListOfValuesInfo lovInfo = lov.getListOfValues();
                System.out.println("lovInfo:" + lovInfo);
                String realVal = (String) lovInfo.getRealValue(displayValue);
                return realVal;
            }
        } catch (TCException e) {
        	System.out.println("取lov的真实值warn："+e);
        }
        return null;
    }
}
