import com.matrixone.apps.cpd.json.JSONObject;
import com.matrixone.apps.domain.DomainObject;
import com.matrixone.apps.domain.util.*;
import matrix.db.Context;
import matrix.db.JPO;
import matrix.util.StringList;

import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.logging.Logger;

public class SxChange_mxJPO {

    private static final Logger LOGGER = Logger.getLogger(SxChange_mxJPO.class.getName());

    public MapList getPocId(Context context, String[] args) throws Exception {
        HashMap map = (HashMap) JPO.unpackArgs(args);
//        System.out.println("map :" + map.toString());

        MapList mapList = new MapList();

        String partId = (String) map.get("parentOID");

        String getCheckPOC = "print bus " + partId + " select from[POCToProcess].to.id dump |";

        System.out.println("11111");
        String checkPOCInfo = MqlUtil.mqlCommand(context,getCheckPOC);
        System.out.println("2222");
        System.out.println(checkPOCInfo);
        if(checkPOCInfo.isEmpty())
        {
            return mapList;
        }
        String[] pocInfoSplit = checkPOCInfo.split("\\|");


        System.out.println("33333");
        if(pocInfoSplit.length > 0)
        {
            for (int i = 0; i < pocInfoSplit.length; i++) {
                Map<String,String> itemMap = new HashMap<>();
                System.out.println("99999999");
                System.out.println(pocInfoSplit[i]);
                itemMap.put("id",pocInfoSplit[i]);
                mapList.add(itemMap);
            }
        }
        return mapList;
    }

    public StringList getProcessNumber(Context context, String[] args) throws Exception {
        HashMap hashMap=(HashMap) JPO.unpackArgs(args);
        System.out.println("444444");
        List list=(List) hashMap.get("objectList");
        System.out.println(list.toString());
        StringList stringList = new StringList();
        System.out.println("555555");
        for (int i = 0; i < list.size(); i++) {
            stringList.add(String.valueOf((i + 1)));
        }

        return stringList;
    }

    public String getDepartmentInfo(Context context, String[] args) throws FrameworkException {
        StringBuffer htmlString = new StringBuffer();
        try {
            HashMap programMap = (HashMap) JPO.unpackArgs(args);

            HashMap fieldMap = (HashMap) programMap.get("fieldMap");
            String attNameStr = (String) fieldMap.get("name");
            HashMap requestMap = (HashMap) programMap.get("requestMap");

            LOGGER.info("====> " + attNameStr);
            LOGGER.info("====> " + fieldMap.get("settings"));
            LOGGER.info("====> " + requestMap);

            System.out.println("getProvince");

            if(!requestMap.containsKey("mode")) {
                htmlString.append("<script type=\"text/javascript\" src=\"../SxCentral/js/getProcessingTechnologyInfo.js\"></script>");
                htmlString.append("<script type=\"text/javascript\" src=\"../SxCentral/js/jquery.js\"></script>");

                Map settings = (Map) fieldMap.get("settings");
                String AttributelinkProcessingTechnology = (String) settings.get("AttributelinkProcessingTechnology");
                String attNameAdmin = (String) settings.get("Admin Type");
                String attName = PropertyUtil.getSchemaProperty(context,attNameAdmin);

                LOGGER.info("===>" + AttributelinkProcessingTechnology);
                LOGGER.info("===>" + attNameAdmin);
                LOGGER.info("===>" + attName);

                htmlString.append("<select  id='"+attNameStr+"'  name='" + attNameStr + "' onchange=\"javascript=insertProcessingTechnology(this,'" + AttributelinkProcessingTechnology + "');\"><option value=''></option>");

                String attRangeMql = "print att "+attName+" select range dump |";
                String resultattRange = MqlUtil.mqlCommand(context, attRangeMql);

                String newAttRange = resultattRange.replaceAll("=", "");
                String[] arrRange = newAttRange.split("\\|");

                for(int i = 0;i<arrRange.length;i++){
                    LOGGER.info("===>" + "emxFramework.Range."+attName+"."+arrRange[i].trim().replace(" ", "_"));
                    LOGGER.info("===>" + context.getLocale());

                    String proeprties = EnoviaResourceBundle.getProperty(context, "emxFrameworkStringResource", new Locale("zh_CN"), "emxFramework.Range."+attName+"."+arrRange[i].trim().replace(" ", "_"));
                    htmlString.append("<option value='" + arrRange[i].trim() + "'>" + proeprties + "</option>");
                }
                htmlString.append("</select>");
            }
        }catch(Exception e) {
            e.printStackTrace();
        }
        return htmlString.toString();
    }

    public String getProcessingTechnologyJSONObject(Context context, String[] args) throws Exception {
        String department = args[0];

        JSONObject json=new JSONObject();
        String ProcessDepartmentInfo = EnoviaResourceBundle.getProperty(context, "emxSxCentralStringResource", context.getLocale(), department);

        System.out.println("99999999999");
        System.out.println(ProcessDepartmentInfo);

        try {
            String[] processDepartment = ProcessDepartmentInfo.split(" ");

            for(int i = 0; i < processDepartment.length; i++){
                //emxSxCentral.Label.LogisticsSpecialist
                String TestCityRange = EnoviaResourceBundle.getProperty(context, "emxSxCentralStringResource", context.getLocale(),
                        "emxFramework.Label." + processDepartment[i]);
                json.put(processDepartment[i], TestCityRange);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json.toString();
    }

    public  String getProcessingTechnology(Context context, String[] args) throws Exception {
        HashMap programMap = (HashMap) JPO.unpackArgs(args);

        Map fieldMap = (Map) programMap.get("fieldMap");
        HashMap requestMap = (HashMap)programMap.get("requestMap");

        String attNameStr = (String) fieldMap.get("name");

        StringBuffer htmlString=new StringBuffer();

        System.out.println("City");

        if(!requestMap.containsKey("mode"))
        {
            htmlString.append("<select  id='"+attNameStr+"'  name='"+attNameStr+"'><option value=''></option></select>");
        }

        return htmlString.toString();
    }

    public void updateProcessingTechnology(Context context, String[] args) throws Exception {
        HashMap programMap = (HashMap) JPO.unpackArgs(args);

        Map paramMap = (Map) programMap.get("paramMap");
        Map fieldMap = (Map) programMap.get("fieldMap");
        Map settings = (Map) fieldMap.get("settings");

        String attNameStr = (String) settings.get("Admin Type");
        String attName = PropertyUtil.getSchemaProperty(context,attNameStr);
        String NewValue = (String) paramMap.get("New Value");
        String objectId = (String) paramMap.get("objectId");

        DomainObject domainObject = new DomainObject(objectId);
        domainObject.setAttributeValue(context, attName, NewValue);

    }

    public void updateDepartmentInfo(Context context, String[] args) throws Exception {
        HashMap programMap = (HashMap) JPO.unpackArgs(args);

        Map paramMap = (Map) programMap.get("paramMap");
        Map fieldMap = (Map) programMap.get("fieldMap");
        Map settings = (Map) fieldMap.get("settings");

        String attNameStr = (String) settings.get("Admin Type");
        String attName = PropertyUtil.getSchemaProperty(context,attNameStr);
        String NewValue = (String) paramMap.get("New Value");
        String objectId = (String) paramMap.get("objectId");

        DomainObject domainObject = new DomainObject(objectId);
        domainObject.setAttributeValue(context, attName, NewValue);
    }
}
