/*
 * -----------------------------------------------------------------------------
 * COPYRIGHT (C) 2019, FONTEVA, INC.
 * ALL RIGHTS RESERVED.
 *
 * ALL INFORMATION CONTAINED HEREIN IS, AND REMAINS THE PROPERTY OF FONTEVA
 * INCORPORATED AND ITS SUPPLIERS, IF ANY. THE INTELLECTUAL AND TECHNICAL
 * CONCEPTS CONTAINED HEREIN ARE PROPRIETARY TO FONTEVA INCORPORATED AND
 * ITS SUPPLIERS AND MAY BE COVERED BY U.S. AND FOREIGN PATENTS, PATENTS IN
 * PROCESS, AND ARE PROTECTED BY TRADE SECRET OR COPYRIGHT LAW. DISSEMINATION
 * OF THIS INFORMATION OR REPRODUCTION OF THIS MATERIAL IS STRICTLY FORBIDDEN
 * UNLESS PRIOR WRITTEN PERMISSION IS OBTAINED FROM FONTEVA, INC.
 * -----------------------------------------------------------------------------
 */

public with sharing class PfmInputController {
    @AuraEnabled(Cacheable=true)
    public static Object search(String searchTerm, String searchQuery, List<String> selectedIds) {
        Boolean customQuery = !String.isEmpty(searchQuery);

        if (customQuery) {
            List<SObject> retVal = new List<SObject>();
            for (String query : searchQuery.split('\\|')) {
                for (SObject rec : PfmSelector.parseRun(query, searchTerm + '%')) {
                    retVal.add(rec);
                }
            }
            return retVal;
        } else {
            List<LookupSearchResult> results = new List<LookupSearchResult>();
            List<Account> accounts = PfmSelector.parseRun(
                'Select Id, Name, BillingCity From Account Where Name like {0}',
                searchTerm + '%'
            );
            // Extract Accounts & convert them into LookupSearchResult
            for (Account account : accounts) {
                results.add(
                    new LookupSearchResult(
                        account.Id,
                        'Account',
                        'standard:account',
                        account.Name,
                        'Account • ' + account.BillingCity
                    )
                );
            }
            return results;
        }
    }

    /**
     * Class used to serialize a single Lookup search result item
     * The Lookup controller returns a List<LookupSearchResult> when sending search result back to Lightning
     */
    public class LookupSearchResult {
        private Id id;
        private String sObjectType;
        private String icon;
        private String title;
        private String subtitle;

        public LookupSearchResult(Id id, String sObjectType, String icon, String title, String subtitle) {
            this.id = id;
            this.sObjectType = sObjectType;
            this.icon = icon;
            this.title = title;
            this.subtitle = subtitle;
        }

        @AuraEnabled
        public Id getId() {
            return id;
        }

        @AuraEnabled
        public String getSObjectType() {
            return sObjectType;
        }

        @AuraEnabled
        public String getIcon() {
            return icon;
        }

        @AuraEnabled
        public String getTitle() {
            return title;
        }

        @AuraEnabled
        public String getSubtitle() {
            return subtitle;
        }
    }
}
