global class ExportHistoryBatchProcess implements Database.Batchable<sObject>, Database.stateful, Database.RaisesPlatformEvents {
    public List<Export__c> expObjList;
    public Export__c expObj;
    public String expId;
    public String queryString;
    public String queryString1;
    public boolean exceptionRaised = false;
    public list<string> globallist = new List<string>();
    public string objectname;
    // public string subhpromo;
    // public Map<String,String> subhpromoMap;
    // public List<string> subhisid;
    global ExportHistoryBatchProcess() {
        // subhpromo='';
        // subhpromoMap = new Map<String,String>();
        //  subhisid = new List<String>();

        expObjList = [
            SELECT
                Id,
                Name,
                Export_Type__c,
                Exported_By__c,
                Export_HIstory_Processed__c,
                DSP__c,
                Preferred_Genre__c,
                Exported_Record_Count__c,
                Mailing_List_Name__c,
                Form_ID__c,
                Promo_ID__c,
                Form_Tool_Name__c,
                Mailing_List_ID__c,
                IsPickedForProcess__c,
                Age_Range__c,
                Address__c,
                Birthdate__c,
                Birthday__c,
                Created_Date__c,
                createddate,
                Custom_Fields_10__c,
                Custom_Fields_11__c,
                Custom_Fields_12__c,
                Custom_Fields_13__c,
                Custom_Fields_14__c,
                Custom_Fields_15__c,
                Custom_Fields_16__c,
                Custom_Fields_17__c,
                Custom_Fields_18__c,
                Custom_Fields_19__c,
                Custom_Fields_1__c,
                Custom_Fields_20__c,
                Custom_Fields_2__c,
                Custom_Fields_3__c,
                Custom_Fields_4__c,
                Custom_Fields_5__c,
                Custom_Fields_6__c,
                Custom_Fields_7__c,
                Custom_Fields_8__c,
                Custom_Fields_9__c,
                Email__c,
                Fan_City__c,
                Fan_Country__c,
                Fan_State__c,
                First_Name__c,
                Form_Name__c,
                Gender__c,
                Language__c,
                Last_Name__c,
                Mobile_Phone__c,
                Twitter_handle__c,
                Query__c,
                FormName__c,
                Instagram_Handle__c,
                Postal_code_1__c,
                Active_check__c,
                PromoID__c,
                Promo_Description__c
            FROM Export__c
            WHERE
                Export_HIstory_Processed__c = FALSE
                AND is_ready_to_download__c = TRUE
                AND Is_Already_Downloaded__c = TRUE
                AND IsPickedForProcess__c = FALSE
            ORDER BY createddate ASC
            LIMIT 1
        ];

        if (expObjList.size() > 0) {
            for (Export__c exp : expObjList) {
                /*if(exp.Export_Type__c=='Mailing List'){
      if(exp.Promo_ID__c!=''){
        objectname = 'Subscription_History__c';
                expObj = exp;
        }
        else{
        objectname = 'Subscription__c';
                expObj = exp;
        }
            }
      else{
      objectname = 'Form_Response__c';
                expObj = exp;
      }*/

                expObj = exp;
            }

            queryString = expObj.Query__c;

            queryString1 = formQuery();
        } else {
            /* Dummy query to avoid exception - Do Not Change */
            queryString1 = 'SELECT Id,Name FROM Export__c where Name =\'EXP_0000\' limit 1';
        }
    }

    global database.QueryLocator start(database.BatchableContext bc) {
        return database.getQueryLocator(queryString1);
    }

    global void execute(Database.BatchableContext bc, List<Sobject> scope) {
        if (expobj.IsPickedForProcess__c == false) {
            expobj.IsPickedForProcess__c = true;
            update expobj;
        }
        List<Subscription__c> subList = new List<Subscription__c>();
        List<Subscription_History__c> subhisList = new List<Subscription_History__c>();
        List<Form_Response__c> frmList = new List<Form_Response__c>();
        List<Export_History__c> expHistList = new List<Export_History__c>();

        try {
            //if(scope.size()>0){
            if (expObj.Export_Type__c == 'Mailing List') {
                if (expObj.Query__c.contains('Subscription__c')) {
                    subList = (List<Subscription__c>) scope;
                    for (Subscription__c sub : subList) {
                        Export_History__c exHist = new Export_History__c();
                        exHist.Subscription_Id__c = sub.id;
                        exHist.Export_Type__c = expObj.Export_Type__c;
                        if (expObj.Mailing_List_Name__c == true)
                            exHist.Mailing_List_Name__c = sub.Mailing_List_Name__c;

                        if (expObj.Created_Date__c == true)
                            exHist.Created_Date__c = String.valueOf(sub.CreatedDate);

                        if (expObj.Active_check__c == true)
                            exHist.Active__c = String.valueOf(sub.Active__c);

                        if (expObj.Email__c == true)
                            exHist.Email__c = sub.Fan_ID__r.Email__c;

                        if (expObj.First_Name__c == true)
                            exHist.First_Name__c = sub.Fan_ID__r.First_Name__c;

                        if (expObj.Last_Name__c == true)
                            exHist.Last_Name__c = sub.Fan_ID__r.Last_Name__c;

                        if (expObj.Age_Range__c == true)
                            exHist.Age_Range__c = sub.Fan_ID__r.Age_Range__c;

                        if (expObj.Gender__c == true)
                            exHist.Gender__c = sub.Fan_ID__r.Gender__c;

                        if (expObj.Mobile_Phone__c == true)
                            exHist.Mobile_Phone__c = sub.Fan_ID__r.Mobile_Phone__c;

                        if (expObj.Twitter_handle__c == true)
                            exHist.Twitter_handle__c = sub.Fan_ID__r.Twitter_handle__c;

                        if (expObj.Fan_City__c == true)
                            exHist.City__c = sub.Fan_ID__r.City__c;

                        if (expObj.Fan_Country__c == true)
                            exHist.Country__c = sub.Fan_ID__r.Country_Region__c;

                        if (expObj.Fan_State__c == true)
                            exHist.State__c = sub.Fan_ID__r.State__c;

                        if (expObj.Postal_Code_1__c == true)
                            exHist.Postal_Code__c = sub.Fan_ID__r.Postal_Code__c;

                        if (expObj.Language__c == true)
                            exHist.Language__c = sub.Fan_ID__r.Preferred_Language__c;

                        if (expObj.DSP__c == true)
                            exHist.DSP__c = sub.DSP__c;

                        if (expObj.Preferred_Genre__c == true)
                            exHist.Preferred_Genre__c = sub.Preferred_Genre__c;

                        if (expObj.Address__c == true) {
                            exHist.Address_1__c = sub.Fan_ID__r.Address_1__c;
                            exHist.Address_2__c = sub.Fan_ID__r.Address_2__c;
                        }
                        if (expObj.Instagram_Handle__c == true)
                            exHist.Instagram_Handle__c = sub.Fan_ID__r.Instagram_Handle__c;

                        if (expObj.Birthday__c == true)
                            exHist.Birthday__c = sub.Fan_ID__r.Birthday_mm_dd__c;

                        if (expObj.Birthdate__c == true)
                            exHist.Birthdate__c = String.valueOf(sub.Fan_ID__r.Birthdate__c);

                        if (expObj.Twitter_Handle__c == true)
                            exHist.Twitter_Handle__c = sub.Fan_ID__r.Twitter_Handle__c;

                        if (expObj.Promo_Description__c == true)
                            exHist.Promo_Description__c = sub.Promo_Description_Value__c;

                        if (expObj.PromoID__c == true)
                            exHist.PromoID__c = sub.Promo_ID_Value__c;

                        exHist.Fan_ID__c = sub.Fan_ID__c;
                        Id RecTypeId = Schema.SObjectType.Export_History__c.getRecordTypeInfosByName()
                            .get('MailingList Export')
                            .getRecordTypeId();
                        exHist.Recordtypeid = RecTypeId;
                        exHist.Export_ID__c = expObj.ID;
                        exHist.Mailing_List_ID__c = expObj.Mailing_List_ID__c;
                        // Additional Tracking Fields for Mailing List Table
                        exHist.Exported_By__c = expObj.Exported_By__c;
                        exHist.Date_Exported__c = expObj.createddate.date();

                        expHistList.add(exHist);
                    }
                } else if (expObj.Query__c.contains('Subscription_History__c')) {
                    subhisList = (List<Subscription_History__c>) scope;
                    for (Subscription_History__c subh : subhisList) {
                        if (!globallist.contains(subh.Subscription_id__c + subh.Promo_id__r.name)) {
                            globallist.add(subh.Subscription_id__c + subh.Promo_id__r.name);
                            Export_History__c exHist = new Export_History__c();
                            exHist.Subscription_History_Id__c = subh.id;
                            exHist.Export_Type__c = expObj.Export_Type__c;
                            if (expObj.Mailing_List_Name__c == true)
                                exHist.Mailing_List_Name__c = subh.Mailing_List_Name__c;

                            if (expObj.Created_Date__c == true)
                                exHist.Created_Date__c = String.valueOf(subh.CreatedDate);

                            /*if(expObj.Active_check__c==true)
                             exHist.Active__c = String.valueOf(subh.Active__c);*/

                            if (expObj.Email__c == true)
                                exHist.Email__c = subh.Fan_ID__r.Email__c;

                            if (expObj.First_Name__c == true)
                                exHist.First_Name__c = subh.Fan_ID__r.First_Name__c;

                            if (expObj.Last_Name__c == true)
                                exHist.Last_Name__c = subh.Fan_ID__r.Last_Name__c;

                            if (expObj.Age_Range__c == true)
                                exHist.Age_Range__c = subh.Fan_ID__r.Age_Range__c;

                            if (expObj.Gender__c == true)
                                exHist.Gender__c = subh.Fan_ID__r.Gender__c;

                            if (expObj.Mobile_Phone__c == true)
                                exHist.Mobile_Phone__c = subh.Fan_ID__r.Mobile_Phone__c;

                            if (expObj.Twitter_handle__c == true)
                                exHist.Twitter_handle__c = subh.Fan_ID__r.Twitter_handle__c;

                            if (expObj.Fan_City__c == true)
                                exHist.City__c = subh.Fan_ID__r.City__c;

                            if (expObj.Fan_Country__c == true)
                                exHist.Country__c = subh.Fan_ID__r.Country_Region__c;

                            if (expObj.Fan_State__c == true)
                                exHist.State__c = subh.Fan_ID__r.State__c;

                            if (expObj.Postal_Code_1__c == true)
                                exHist.Postal_Code__c = subh.Fan_ID__r.Postal_Code__c;

                            if (expObj.Language__c == true)
                                exHist.Language__c = subh.Fan_ID__r.Preferred_Language__c;

                            if (expObj.Address__c == true) {
                                exHist.Address_1__c = subh.Fan_ID__r.Address_1__c;
                                exHist.Address_2__c = subh.Fan_ID__r.Address_2__c;
                            }
                            if (expObj.Instagram_Handle__c == true)
                                exHist.Instagram_Handle__c = subh.Fan_ID__r.Instagram_Handle__c;

                            if (expObj.Birthday__c == true)
                                exHist.Birthday__c = subh.Fan_ID__r.Birthday_mm_dd__c;

                            if (expObj.Birthdate__c == true)
                                exHist.Birthdate__c = String.valueOf(subh.Fan_ID__r.Birthdate__c);

                            if (expObj.Twitter_Handle__c == true)
                                exHist.Twitter_Handle__c = subh.Fan_ID__r.Twitter_Handle__c;

                            if (expObj.Promo_Description__c == true)
                                exHist.Promo_Description__c = subh.Promo_Description_Value__c;

                            if (expObj.PromoID__c == true)
                                exHist.PromoID__c = subh.Promo_ID_Value__c;

                            exHist.Fan_ID__c = subh.Fan_ID__c;
                            Id RecTypeId = Schema.SObjectType.Export_History__c.getRecordTypeInfosByName()
                                .get('MailingList Export')
                                .getRecordTypeId();
                            exHist.Recordtypeid = RecTypeId;
                            exHist.Export_ID__c = expObj.ID;
                            exHist.Mailing_List_ID__c = expObj.Mailing_List_ID__c;
                            // Additional Tracking Fields for Mailing List Table
                            exHist.Exported_By__c = expObj.Exported_By__c;
                            exHist.Date_Exported__c = expObj.createddate.date();

                            expHistList.add(exHist);
                        }
                    }
                }
            } else {
                frmList = (List<Form_Response__c>) scope;
                for (Form_Response__c frm : frmList) {
                    Export_History__c exHist = new Export_History__c();
                    exHist.Form_Response_Id__c = frm.id;
                    exhist.Export_Type__c = expObj.Export_Type__c;
                    if (expObj.Form_Name__c == true)
                        exHist.Form_Name__c = frm.Form_Id__r.Form_Name__c;

                    if (expObj.Created_Date__c == true)
                        exHist.Created_Date__c = String.valueOf(frm.CreatedDate);

                    if (expObj.Email__c == true)
                        exHist.Email__c = frm.Email__c;

                    if (expObj.First_Name__c == true)
                        exHist.First_Name__c = frm.First_Name__c;

                    if (expObj.Last_Name__c == true)
                        exHist.Last_Name__c = frm.Last_Name__c;

                    if (expObj.Age_Range__c == true)
                        exHist.Age_Range__c = frm.Age_Range__c;

                    if (expObj.Gender__c == true)
                        exHist.Gender__c = frm.Gender__c;

                    if (expObj.Mobile_Phone__c == true)
                        exHist.Mobile_Phone__c = frm.Mobile_Phone__c;

                    if (expObj.Twitter_handle__c == true)
                        exHist.Twitter_handle__c = frm.Twitter_handle__c;

                    if (expObj.Fan_City__c == true)
                        exHist.City__c = frm.City__c;

                    if (expObj.Fan_Country__c == true)
                        exHist.Country__c = frm.Country_Region__c;

                    if (expObj.Fan_State__c == true)
                        exHist.State__c = frm.State__c;

                    if (expObj.Postal_Code_1__c == true)
                        exHist.Postal_Code__c = frm.Postal_Code__c;

                    if (expObj.Language__c == true)
                        exHist.Language__c = frm.preferred_Language__c;

                    if (expObj.DSP__c == true)
                        exHist.Language__c = frm.DSP__c;

                    if (expObj.Preferred_Genre__c == true)
                        exHist.Language__c = frm.Preferred_Genre__c;

                    if (expObj.Address__c == true) {
                        exHist.Address_1__c = frm.Address_1__c;
                        exHist.Address_2__c = frm.Address_2__c;
                    }

                    if (expObj.Birthday__c == true)
                        exHist.Birthday__c = frm.Birthday_mm_dd__c;

                    if (expObj.Birthdate__c == true)
                        exHist.Birthdate__c = String.valueOf(frm.Birthday__c);

                    if (expObj.Twitter_handle__c == true)
                        exHist.Twitter_handle__c = frm.Twitter_handle__c;

                    if (expObj.Custom_Fields_1__c == true)
                        exHist.Custom_Fields_1__c = frm.Custom_Form_Field1__c;

                    if (expObj.Custom_Fields_2__c == true)
                        exHist.Custom_Fields_2__c = frm.Custom_Form_Field2__c;

                    if (expObj.Custom_Fields_3__c == true)
                        exHist.Custom_Fields_3__c = frm.Custom_Form_Field3__c;

                    if (expObj.Custom_Fields_4__c == true)
                        exHist.Custom_Fields_4__c = frm.Custom_Form_Field4__c;

                    if (expObj.Custom_Fields_5__c == true)
                        exHist.Custom_Fields_5__c = frm.Custom_Form_Field5__c;

                    if (expObj.Custom_Fields_6__c == true)
                        exHist.Custom_Fields_6__c = frm.Custom_Form_Field1__c;

                    if (expObj.Custom_Fields_7__c == true)
                        exHist.Custom_Fields_7__c = frm.Custom_Form_Field7__c;

                    if (expObj.Custom_Fields_8__c == true)
                        exHist.Custom_Fields_8__c = frm.Custom_Form_Field8__c;

                    if (expObj.Custom_Fields_9__c == true)
                        exHist.Custom_Fields_9__c = frm.Custom_Form_Field9__c;

                    if (expObj.Custom_Fields_10__c == true)
                        exHist.Custom_Fields_10__c = frm.Custom_Form_Field10__c;

                    if (expObj.Custom_Fields_11__c == true)
                        exHist.Custom_Fields_11__c = frm.Custom_Form_Field11__c;

                    if (expObj.Custom_Fields_12__c == true)
                        exHist.Custom_Fields_12__c = frm.Custom_Form_Field12__c;

                    if (expObj.Custom_Fields_13__c == true)
                        exHist.Custom_Fields_13__c = frm.Custom_Form_Field13__c;

                    if (expObj.Custom_Fields_14__c == true)
                        exHist.Custom_Fields_14__c = frm.Custom_Form_Field14__c;

                    if (expObj.Custom_Fields_15__c == true)
                        exHist.Custom_Fields_15__c = frm.Custom_Form_Field15__c;

                    if (expObj.Custom_Fields_16__c == true)
                        exHist.Custom_Fields_16__c = frm.Custom_Form_Field16__c;

                    if (expObj.Custom_Fields_17__c == true)
                        exHist.Custom_Fields_17__c = frm.Custom_Form_Field17__c;

                    if (expObj.Custom_Fields_18__c == true)
                        exHist.Custom_Fields_18__c = frm.Custom_Form_Field18__c;

                    if (expObj.Custom_Fields_19__c == true)
                        exHist.Custom_Fields_19__c = frm.Custom_Form_Field19__c;

                    if (expObj.Custom_Fields_20__c == true)
                        exHist.Custom_Fields_20__c = frm.Custom_Form_Field20__c;

                    exHist.Fan_ID__c = frm.Fan_ID__c;
                    Id RecTypeId = Schema.SObjectType.Export_History__c.getRecordTypeInfosByName()
                        .get('FormResponseExport')
                        .getRecordTypeId();
                    exHist.Recordtypeid = RecTypeId;
                    exHist.Export_ID__c = expObj.ID;
                    exHist.Form_ID__c = expObj.Form_ID__c;
                    // Additional Tracking Fields for Form response Table
                    exHist.Exported_By__c = expObj.Exported_By__c;
                    exHist.Date_Exported__c = expObj.createddate.date();
                    exHist.Form_Name__c = expObj.FormName__c;
                    exHist.Form_Tool_Name__c = expObj.Form_Tool_Name__c;

                    expHistList.add(exHist);
                }
            }

            insert expHistList;
            //}
        } catch (Exception e) {
            SMELogger.logError(e);
            exceptionRaised = true;
        }
    }

    global void finish(database.BatchableContext bc) {
        try {
            if (!exceptionRaised) {
                expobj.Export_HIstory_Processed__c = true;
                update expobj;
            }
            System.schedule(
                'ExportHistoryBatchProcess Job 1',
                '0 0 * * * ?',
                new SchedulableExportHistoryBatchProcess()
            );
        } catch (Exception e) {
            //SMELogger.logError(e);
        }
    }

    public String formQuery() {
        string str = '';
        // string hisquery = '';
        if (queryString.contains('Subscription__c') || queryString.contains('Form_Response__c')) {
            String[] arryStr = queryString.split('FROM');
            str = arryStr[0] + ',id,Fan_Id__c FROM ' + arryStr[1];
            //return str;
        } else {
            String[] arryStr = queryString.split('FROM');
            str = arryStr[0] + ',Fan_Id__c FROM ' + arryStr[1];
            /* String[] arryStr1 = hisquery.split('FROM');
        List<Subscription_History__c> subhlist = Database.query(hisquery);
        for(Subscription_History__c sh:subhlist){
        subhpromo = sh.Subscription_id__c+sh.Promo_id__r.name;
        subhpromoMap.put(subhpromo,sh.id);
        }
        subhisid = subhpromoMap.values();
        str = arryStr[0]+' FROM Subscription_History__c where id IN:subhisid';
        */
        }
        return str;
    }
}
