public with sharing class Export_ComboBox_Con {
    public String targetField;
    public List<Form__c> forms { get; set; }
    public String labelField { get; set; }
    public String labelField1 { get; set; }
    public String valueField { get; set; }
    public String activeField { get; set; }
    public String sObjVal { get; set; }
    public Integer randomJsIden { get; set; }

    public Export_ComboBox_Con() {
        randomJsIden = getRandomNumber(1000000);
    }

    /*
     *Setter method to set the targetField
     ***/
    public void setTargetField(String targetField) {
        if (targetField == null) {
            this.targetField = targetField;
        }
    }

    /*
     *Random number generator to change the js function name if multiple components us
     ***/
    private Integer getRandomNumber(Integer size) {
        Double d = math.random() * size;
        return d.intValue();
    }

    /*
     *This method queries data according to the passed parameters
     ***/
    @RemoteAction
    public static List<Map<String, String>> getData(
        String sObjVal,
        String labelField,
        String labelField1,
        String valueField,
        String param,
        String filterField,
        String filterField2,
        String filterField3,
        String match,
        String match2,
        String match3,
        String activeField,
        String orderByField
    ) {
        param = String.escapeSingleQuotes(param);
        match = String.escapeSingleQuotes(match);
        match2 = String.escapeSingleQuotes(match2);
        match3 = String.escapeSingleQuotes(match3);

        system.debug('param is ' + param);
        system.debug('match is ' + match);
        system.debug('match2 is ' + match2);
        system.debug('match3 is ' + match3);
        String query;
        if (sObjVal == 'Form__c') {
            query =
                'select ' +
                valueField +
                ',' +
                labelField +
                ',' +
                labelField1 +
                ' from ' +
                sObjVal +
                ' where  (' +
                labelField +
                ' != null' +
                ' or ' +
                labelField1 +
                ' != null)';
        } else {
            query =
                'select ' +
                valueField +
                ',' +
                labelField +
                ',' +
                labelField1 +
                ' from ' +
                sObjVal +
                ' where ' +
                labelField +
                ' like \'%' +
                param +
                '%\'';
        }

        if (match != null && match != '') {
            query += ' and ' + filterField + '=\'' + match + '\'';
        }
        if (match2 != null && match2 != '') {
            query += ' and ' + filterField2 + '=\'' + match2 + '\'';
        }
        if (match3 != null && match3 != '') {
            query += ' and ' + filterField3 + '=\'' + match3 + '\'';
        }
        if (activeField != null && activeField != '') {
            query += ' and ' + activeField + '=true';
        }
        if (sObjVal == 'Form__c') {
            if (orderByField != null && orderByField != '') {
                query += ' ORDER BY ' + orderByField + ' DESC';
            }
        } else {
            if (orderByField != null && orderByField != '') {
                query += ' ORDER BY ' + orderByField;
            }
        }

        system.debug('dynamic query--> ' + query);
        List<SObject> results = Database.query(query);

        /* Changes to handle migration forms */
        List<String> nonmigforms = new List<String>();
        Set<String> migforms = new Set<String>();
        List<String> remformstrim = new List<String>();
        List<String> finalmigforms = new List<String>();
        Map<String, String> midformnamemap = new Map<String, String>();

        Map<String, String> uniqueValues = new Map<String, String>();
        List<Map<String, String>> returnResults = new List<Map<String, String>>();

        for (SObject o : results) {
            String label, label4, label3, value;

            if (labelField1 == 'Migration_Id__c') {
                value = (String) o.get(valueField);
                label3 = (String) o.get(labelField);
                if (String.IsBlank(label3)) {
                    label3 = 'null';
                }
                label4 = (String) o.get(labelField1);
                if (String.isBlank(label4)) {
                    label4 = (String) o.get(valueField);
                    finalmigforms.add(label4);
                    uniqueValues.put(value, label3);
                } else if (label4 != null && !label4.contains('mig')) {
                    nonmigforms.add(label4);
                    finalmigforms.add(label4);
                    label = label4 + ' - ' + label3;
                    uniqueValues.put(value, label);
                }
            }
        }

        for (SObject o : results) {
            String label, label4, label3, value;

            if (labelField1 == 'Migration_Id__c') {
                value = (String) o.get(valueField);
                label3 = (String) o.get(labelField);
                if (String.IsBlank(label3)) {
                    label3 = 'null';
                }
                label4 = (String) o.get(labelField1);

                if (label4 != null && label4.contains('mig')) {
                    String a = label4.substringAfter('-');
                    system.debug('a=' + a);
                    if (finalmigforms.size() > 0 && !finalmigforms.contains(a)) {
                        finalmigforms.add(label4);
                        label = label4 + ' - ' + label3;
                        uniqueValues.put(value, label);
                    }
                }
            }
        }

        for (SObject o : results) {
            String label, value;

            if (labelField1 == 'Migration_Id__c') {
                value = (String) o.get(valueField);
                if (labelField.contains('.')) {
                    String objName = labelField.substringBefore('.');
                    String fieldName = labelField.substringAfter('.');
                    String objName1 = labelField1.substringBefore('.');
                    String fieldName1 = labelField1.substringAfter('.');
                    String label1 = (String) o.getSObject(objName).get(fieldName);
                    String label2 = (String) o.getSObject(objName1).get(fieldName1);

                    if (label2 == '' || label2 == null) {
                        label = label1;
                    } else {
                        label = label2 + ' - ' + label1;
                    }
                } else {
                    String label3 = (String) o.get(labelField);
                    String label4 = (String) o.get(labelField1);
                    if (String.isBlank(label4)) {
                        uniqueValues.put(value, label3);
                    }
                }
            } else {
                if (labelField.contains('.')) {
                    String objName = labelField.substringBefore('.');
                    String fieldName = labelField.substringAfter('.');
                    label = (String) o.getSObject(objName).get(fieldName);
                    System.debug('LabelField for ML if DOT::' + label);
                } else {
                    label = (String) o.get(labelField);
                    System.debug('LabelField for ML::' + label);
                }

                if (valueField.contains('.')) {
                    String objName = valueField.substringBefore('.');
                    String fieldName = valueField.substringAfter('.');
                    value = (String) o.getSObject(objName).get(fieldName);
                } else {
                    value = (String) o.get(valueField);
                }
                uniqueValues.put(value, label);
                System.debug('LabelField for ML::' + uniqueValues);
            }
        }

        Set<String> values = uniqueValues.keySet();
        for (String v : values) {
            Map<String, String> record = new Map<String, String>{ 'label' => uniqueValues.get(v), 'value' => v };
            returnResults.add(record);
            System.debug('Final Result::' + returnResults);
        }

        return returnResults;
    }
}
