import GraphQLService from './GraphQLService';

export interface HomeSectionData{
  id: number
  title: string
  desc : string
 /*  fr_inspiration_galleries?: {
      id : number
      title : string
  }
  fr_past_work_galleries?: {
      id : number
      title : string
  } */
}
export interface HomeSectionOPData {
  HouseSectionFind?:{
    rows?:HomeSectionData[],
    total:number,
    pages:number
  }
}
export interface HomeSectionIdOPData {
  HouseSectionFindOneById?:HomeSectionData
}
export interface HomeSectionCreateOPData {
  HouseSectionCreate?:HomeSectionData[]
}

interface SnapshotData {
  message : string
  result : string
  imp : string
  mismatch : string
  notFound : string
  conflict : string
  success : string
  error : string
  alert : string
  warning : string
  notice : string
  info : string
}
export interface HomeSectionUpdateOPData {
  HouseSectionUpdate?:{
    snapshot: SnapshotData
    affected : number
    affectedRows ?:HomeSectionData[]
  }
}
export interface HomeSectionUpsertOPData {
  HouseSectionUpsert?:HomeSectionData
}
export interface HomeSectionDeleteOPData {
  HouseSectionDelete?:{
    snapshot: SnapshotData
    affected : number
  }
}

export interface UserFileUploadIPData{
  attachment: File[] | undefined,
  input:{
    ref_id : string
    file_field : string
  }
}

interface fileUpload{
  file_field: string,
  id: string,
  ref_id : string,
  file_name : string
  access_url ?:{
    direct : string
    thumb :string
  }
}

export interface UserFileUploadOPData{
  UserFileUpload?: fileUpload
}

class HomeSectionService extends GraphQLService {

    constructor() {
      super();
    }
    
  readonly houseSectionQuery =`query Rows($filter: HouseSectionFindInput!) {
    HouseSectionFind(filter: $filter) {
      rows {
        id
        title
        desc
      }
      total
      pages
    }
  }`;
  
    getHomeSection(pageSize:number, start:number,sortorder:string):Promise<HomeSectionOPData |null | undefined>{
      
      const variables = {
          "filter": {
            "order": {
            "title": sortorder
            },
            "skip": start,
            "take": pageSize
          }
      };
  
      return this.execute(this.houseSectionQuery, variables); 
    }

    getHomeSections():Promise<HomeSectionOPData |null | undefined>{
      const variables = {
          "filter": {
            "order": {
              "id": "DESC"
            },
          }
      };
  
      return this.execute(this.houseSectionQuery, variables); 
    }

    getHomeSectionById(Id: number):Promise<HomeSectionIdOPData |null | undefined>{
      const Query =`query HouseSectionFindOneById($input: HouseSectionFindOneByIdInput!) {
                      HouseSectionFindOneById(input: $input) {
                        id
                        title
                        desc
                      }
                    }`;
      const variables = {
          "input": {
            "id": Id
          }
      };
  
      return this.execute(Query, variables); 
    }

    createHomeSection(HomeSectionINData:HomeSectionData):Promise<HomeSectionCreateOPData |null | undefined>{
      const Query =`mutation HouseSectionCreate($input: [HouseSectionCreateInput!]!) {
                    HouseSectionCreate(input: $input) {
                      id
                      title
                      desc
                    }
                  }`;
      const variables = {
        "input": [
          {
            "desc": HomeSectionINData.desc,
            "title": HomeSectionINData.title
          }
        ]
      };
  
      return this.mutate<HomeSectionCreateOPData>(Query, variables); 
    }

    updateHomeSection(HomeSectionINData: HomeSectionData):Promise<HomeSectionUpdateOPData |null | undefined>{
      const Query =`mutation HouseSectionUpdate($input: HouseSectionUpdateInput!) {
                    HouseSectionUpdate(input: $input) {
                       snapshot {
                        message
                        result
                        imp
                        mismatch
                        notFound
                        conflict
                        success
                        error
                        alert
                        warning
                        notice
                        info
                      }
                      affected
                      affectedRows {
                        id
                        title
                        desc
                      
                      }
                    }
                  }`;
            const variables = {
              "input": {
                "sets": {
                  "desc": HomeSectionINData.desc,
                  "title": HomeSectionINData.title
                },
                "where": [
                  {
                    "id": {
                      "equal": `${HomeSectionINData.id}`
                    }
                  }
                ]
              }
            };
  
      return this.mutate<HomeSectionUpdateOPData>(Query, variables); 
    }

    upsertHomeSection(HomeSectionINData: HomeSectionData):Promise<HomeSectionUpsertOPData |null | undefined>{
      const Query =`mutation HouseSectionUpsert($input: [HouseSectionUpsertInput!]!) {
                    HouseSectionUpsert(input: $input) {
                      id
                      title
                      desc
                    }
                  }`;
            const variables = {
              "input": [
                {
                  "desc": null,
                  "id": null,
                  "title": null
                }
              ]
            };
  
      return this.mutate<HomeSectionUpsertOPData>(Query, variables); 
    }

    deleteHomeSection(Id: number):Promise<HomeSectionDeleteOPData |null | undefined>{
      const Query =`mutation HouseSectionDelete($input: HouseSectionDeleteInput!) {
                    HouseSectionDelete(input: $input) {
                       snapshot {
                        message
                        result
                        imp
                        mismatch
                        notFound
                        conflict
                        success
                        error
                        alert
                        warning
                        notice
                        info
                      }
                      affected
                    }
                  }`;
            const variables = {
              input:{
                "where": [
                  {
                    "id": {
                      "equal": `${Id}`
                    }
                  }
                ]
              }
            };
  
      return this.mutate<HomeSectionDeleteOPData>(Query, variables); 
    }

    getHomeSectionIdByTitle(title: string):Promise<HomeSectionOPData |null | undefined>{
      const variables = {
          "filter": {
            "where": [
              {
                "title": {
                  "like": title
                }
              }
            ]
          }
      };
  
      return this.execute(this.houseSectionQuery, variables); 
    }

    sortAscGetHomeEction(sortOrder:string,pageSize:number, start:number):Promise<HomeSectionOPData |null | undefined>{
      const Query =`query Rows($filter: HouseSectionFindInput!) {
                    HouseSectionFind(filter: $filter) {
                      rows {
                        id
                        title
                        desc
                      }
                      total
                      pages
                    }
                  }`;
      const variables = {
        "filter": {
          "order": {
            "title": sortOrder
          },
          "skip": start,
          "take": pageSize
        }
    };

      return this.execute(Query, variables); 
    }
    
}

const homeSectionServiceInstance = new HomeSectionService();
export default homeSectionServiceInstance;