import GraphQLService from './GraphQLService';



 export interface SettingJson {
    SettingJson?:{
        [groupId: string]: {
            [key: string]: Setting;
        }[];
    }
  }

  export interface Setting {
    SettingFind?:{
        rows?:{
            id: number;
            key: string;
            settty_id: number;
            settc_id: number;
            frmfield_id: number;
            device_id: number | null;
            sd_id: number | null;
            sd_name: string | null;
            default_sdv_id: number | null;
            default_sdv_key: string | null;
            default_sdt_key: string | null;
            default_plain_value: string | null;
            access_type: number;
            title: string;
            display_title: string;
            guideline: string;
            fr_setting_preference: {
                id: number;
                plain_value: string;
            }[]
        }[]
    }
  }

  export interface SettingPref {
    SettingPreferenceFind?:{
        rows?:settingspreference
    }
  }

  export interface settingspreference {
    id: number;
    settty_id: number;
    record_id: number | null;
    sett_id: number;
    sett_key: string;
    sd_id: number | null;
    sd_name: string | null;
    sdv_id: number | null;
    sdv_key: string | null;
    sdt_key: string | null;
    plain_value: string | null;
  }

  export interface settingspreferenceUpdateOPData {
    SettingPreferenceUpdate?:{
      snapshot?:{
        error:string
      }
      affected:number
      affectedRows?:settingspreference
    }
  }

  
  

class SettingsService extends GraphQLService {
    constructor() {
      super();
    }

    getSettingsFind(): Promise<Setting | null |undefined> {
        const Query = `query Rows($filter: SettingFindInput!) {
                        SettingFind(filter: $filter) {
                            rows {
                            id
                            key
                            settty_id
                            settc_id
                            frmfield_id
                            device_id
                            sd_id
                            sd_name
                            default_sdv_id
                            default_sdv_key
                            default_sdt_key
                            default_plain_value
                            access_type
                            title
                            display_title
                            guideline
                            fr_setting_preference {
                                id
                                plain_value
                            }
                            }
                        }
                    }`;
    
        const variables = {
                "filter": {
                  "order": {
                    "id": "DESC"
                  }
                }              
            };
     
        return this.execute<Setting>(Query, variables); 
    }  

    SettingPreferenceUpdate(sett_key:string, value:string): Promise<settingspreferenceUpdateOPData |null | undefined> {
      const Query = `mutation SettingPreferenceUpdate($input: SettingPreferenceUpdateInput!) {
                      SettingPreferenceUpdate(input: $input) {
                        snapshot {
                          error
                        }
                        affected
                        affectedRows {
                          id
                          settty_id
                          record_id
                          sett_id
                          sett_key
                          sd_id
                          sd_name
                          sdv_id
                          sdv_key
                          sdt_key
                          plain_value
                        }
                      }
                    }
                  `;
  
      const variables = {
        "input": {
          "sets": {
            "plain_value": value
          },
          "where": [
            {
              "sett_key": {
                "equal": sett_key
              }
            }
          ]
        }
      };
      return this.mutate<settingspreferenceUpdateOPData>(Query, variables); 
    }

    getSettingPrefSettKey(sett_key:string): Promise<SettingPref | null |undefined> {
      const Query = `query Rows($filter: SettingPreferenceFindInput!) {
                      SettingPreferenceFind(filter: $filter) {
                        rows {
                          id
                          settty_id
                          record_id
                          sett_id
                          sett_key
                          sd_id
                          sd_name
                          sdv_id
                          sdv_key
                          sdt_key
                          plain_value
                        }
                      }
                    }`;
  
      const variables = {
              "filter": {
                "where": [
                  {
                    "sett_key": {
                      "equal": sett_key
                    }
                  }
                ]
              }             
          };
   
      return this.execute<SettingPref>(Query, variables); 
  }  

}

const settingsServiceInstance = new SettingsService();
export default settingsServiceInstance;