/*
 * Author: Zhang,Heyang
 * Created Date: 2023/08/07
 * Purpose: get special lookup field infomation
 * Test Class: LexSearchLookupControllerTest
 *
 * */
public with sharing class LexSearchLookupController {

    @AuraEnabled
    public static ResponseBodyLWC getObjectInfo(String objectName ,List<String> searchFieldArray,String searchWhere,String searchField,String searchNameValue){
        ResponseBodyLWC res = new ResponseBodyLWC();
        Map<String, object> data = new Map<String, object>();
        res.entity = data;
        System.debug('objectName  = ' + objectName );
        System.debug('searchFieldArray = ' + searchFieldArray);
        System.debug('searchField = ' + searchField);
        System.debug('searchNameValue = ' + searchNameValue);
        try {
            String soql = 'select ';
            for(String field : searchFieldArray){
                soql += field + ',';
            }
            soql = soql.substring(0,soql.length() - 1);
            soql += ' from ' + objectName;
            if(String.isNotEmpty(searchWhere)){
                soql += ' where ' + searchWhere;
            }
            if(String.isNotEmpty(searchNameValue)){
                if(soql.toLowerCase().contains('where')){
                    soql += ' and ' + searchField + ' like \'%' + searchNameValue + '%\'';
                }else {
                    soql += ' where ' + searchField + ' like \'%' + searchNameValue + '%\'';
                }
            }
            soql += ' limit 2000';
            System.debug('soql = ' + soql);
            List<SObject> objectInfo = Database.query(soql);
            System.debug('objectInfo = ' + objectInfo);
            
            

            Map<String, String> fieldMapWithLabels = new Map<String, String>();
            Map<String, Object> fieldMapWithDatas = new Map<String, Object>();
            List<Map<String, Object>> datas = new List<Map<String, Object>>();
            String formateName = '';
            SObjectType sObjectType = Schema.getGlobalDescribe().get(objectName);
            if (sObjectType != null) {
                Map<String, SObjectField> fieldMap = sObjectType.getDescribe().fields.getMap();
                for (String fieldName : searchFieldArray) {
                    if(fieldName.contains('.')){
                        formateName = fieldName.split('.Name')[0];
                        System.debug('formateName: ' + formateName);
                        if(fieldName.contains('__r')){
                            fieldName = fieldName.split('__r')[0] + '__c';
                            System.debug('fieldName: ' + fieldName);
                        }
                        else{
                            fieldName = fieldName.split('.Name')[0] + 'Id';
                            System.debug('fieldName: ' + fieldName);
                        }
                    }else{
                        formateName = fieldName;
                    }
                    if (fieldMap.containsKey(fieldName) && fieldName != 'Id') {
                        Schema.DescribeFieldResult fieldDescribe = fieldMap.get(fieldName).getDescribe();
                        String fieldLabel = fieldDescribe.getLabel();
                        fieldMapWithLabels.put(formateName, fieldLabel);
                    }
                }
            }
            System.debug('datas: ' + datas);
            System.debug(fieldMapWithLabels);
            data.put('objectInfo', objectInfo);
            //data.put('datas', datas);
            data.put('fieldMapWithLabels', fieldMapWithLabels);
            res.status = 'Success';
            res.code = 200;
            res.msg = '';
            return res;
        } catch (Exception e) {
            return new ResponseBodyLWC('Error', 500, e.getMessage() + ' ' + e.getLineNumber(), '');
        }
    }
}