import GraphQLService from './GraphQLService';

interface PastWorkGallery{
  id: number
  housesec_id : number
  file_image : string
  file_image_url?: {
    thumb : string
    direct : string
  }
  cover : string
  fr_house_section?: {
    id : number
    title : string
  }
}

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
}

interface PastWorkGalleryInsert{
  affected:number,
  id:number,
  ref_group_relation_field_value:number,
  mark_as_main_field:string,
  snapshot:SnapshotData
}

interface PastWorkGallerycreate{
id:number,
ref_group_relation_field_value:number,
mark_as_main_field:string,
housesec_id:number
}

export interface PastWorkGalleryOPData {
  PastWorkGalleryFind?: {
        rows?:PastWorkGallery[]      
        total : number
        pages : number 
    };
}

export interface PastWorkGalleryByIdOPData {
  PastWorkGalleryFindOneById?: PastWorkGallery
}

interface PastWorkGalleryCreateOPData{
  PastWorkGalleryMarkAsMain?: PastWorkGalleryInsert
}

interface InspirationGalleryFileRelocationInput {
  id?: string | null;
  file_name?: string | null;
  ref_id: string;
  affected: number;
  housesec_id:number
}
interface PastWorkGalleryUpdateOPData{
  PastWorkGalleryUpdate?: {
    snapshot:SnapshotData
    affected:number
    affectedRows:PastWorkGallery[]  
  }
}

interface InspirationGalleryFileRelocationItem {
  id: string | null;
  file_name: string | null;
  ref_id: string;
  affected: number;
  snapshort:SnapshotData
}

interface PastWorkGalleryhoseSecUpdateOPData{
  InspirationGalleryFileRelocation:InspirationGalleryFileRelocationItem[]
}

interface PastWorkGalleryUpsertOPData{
  PastWorkGalleryUpsert?: PastWorkGallery
}
interface PastWorkGalleryDeleteOPData {
  PastWorkGalleryDelete?:{
    snapshot: SnapshotData
    affected : number
  }
}

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

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

export interface PastWorkGalleryUploadOPData{
  PastWorkGalleryUpload?: fileUpload[]
}

export interface PastWorkGalleryDelOPData{
  PastWorkGalleryUploadDelete?:{
    file_field : string,
    id : string,
    ref_id : string,
    affected: number,
    snapshot: SnapshotData
  }[]
}

export interface PastWorkGalleryByHomeSection{
  PastWorkGalleryFind?:{
    rows?: PastWorkGallery[]
    total : number
    pages : number 
  }
}

class PastWorkGalleryService extends GraphQLService {

    constructor() {
      super();
    }

    readonly PastWorkGalleryQuery =`query Rows($filter: PastWorkGalleryFindInput!) {
                    PastWorkGalleryFind(filter: $filter) {
                      rows {
                        id
                        housesec_id
                        file_image
                        file_image_url {
                          direct
                          thumb
                        }
                        cover
                        fr_house_section {
                          id
                          title
                        }
                      }
                      total
                      pages
                    }
                  }`;

    getPastWorkGallery(pageSize:number, start:number,sortorder:string):Promise<PastWorkGalleryOPData |null | undefined>{
      
      const variables = {
          "filter": {
            "order": {
              "id":sortorder
            },
            "skip": start,
            "take": pageSize
          }
      };
  
      return this.execute(this.PastWorkGalleryQuery, variables); 
    }

    getPastWorkGalleryById(Id: number):Promise<PastWorkGalleryByIdOPData |null | undefined>{
      const Query =`query PastWorkGalleryFindOneById($input: PastWorkGalleryFindOneByIdInput!) {
                    PastWorkGalleryFindOneById(input: $input) {
                      id
                      housesec_id
                      file_image
                      file_image_url {
                        direct
                        thumb
                      }
                      cover
                      created
                      updated
                      fr_house_section {
                        id
                        title
                      }
                    }
                  }`;
      const variables = {
          "input": {
            "id": Id
          }
      };
  
      return this.execute(Query, variables); 
    }

    createPastWorkGallery(PastWorkGalleryINData: PastWorkGallerycreate,id:number):Promise<PastWorkGalleryCreateOPData |null | undefined>{
      const Query =`mutation PastWorkGalleryMarkAsMain($input: [PastWorkGalleryMarkAsMainInput!]!) {
                    PastWorkGalleryMarkAsMain(input: $input) {
                      affected
                      id
                      ref_group_relation_field_value
                      mark_as_main_field
                      snapshot {
                        message
                        result
                        imp
                        mismatch
                        notFound
                        conflict
                        success
                        error
                        alert
                        warning
                        notice
                        info
                      }
                    }
                  }`;
      const variables = {
          "input": {
            "id": `${id}`,
            "ref_group_relation_field_value": `${PastWorkGalleryINData.housesec_id}`,
            "mark_as_main_field": "COVER"
          }
      };
  
      return this.mutate<PastWorkGalleryCreateOPData>(Query, variables); 
    }

    updatePastWorkGallery(PastWorkGalleryINData: PastWorkGallery, id:number):Promise<PastWorkGalleryUpdateOPData |null | undefined>{
      const Query =`mutation PastWorkGalleryUpdate($input: PastWorkGalleryUpdateInput!) {
                      PastWorkGalleryUpdate(input: $input) {
                        snapshot {
                          message
                          result
                          imp
                          mismatch
                          notFound
                          conflict
                          success
                          error
                          alert
                          warning
                          notice
                          info
                        }
                        affected
                        affectedRows {
                          id
                          housesec_id
                          file_image
                          file_image_url {
                            direct
                            thumb
                          }
                          cover
                          created
                          updated
                          fr_house_section {
                            id
                            title
                          }
                        }
                      }
                    }`;
            const variables = {
              "input": {
                "sets": {
                  "cover": PastWorkGalleryINData.cover,
                },
                "where": [
                  {
                    "id": {
                      "equal": `${id}`
                    }
                  }
                ]
              }
            };
  
      return this.mutate<PastWorkGalleryUpdateOPData>(Query, variables); 
    }

    updatePastWorkGalleryHouseSection(PastWorkGalleryHsecINData: InspirationGalleryFileRelocationInput , id:number,houseId:number):Promise<PastWorkGalleryhoseSecUpdateOPData |null | undefined>{
      const Query =`mutation PastWorkGalleryFileRelocation($input: [PastWorkGalleryFileRelocationInput!]!) {
                    PastWorkGalleryFileRelocation(input: $input) {
                      id
                      ref_id
                      file_name
                      snapshot {
                        message
                        result
                        imp
                        mismatch
                        notFound
                        conflict
                        success
                        error
                        alert
                        warning
                        notice
                        info
                      }
                      affected
                    }
                  }`;
            const variables = {
              "input": [
                {
                  "destination_ref_id":`${PastWorkGalleryHsecINData.housesec_id}`,
                  "file_field": "FILE_IMAGE",
                  "relocation_type": "MOVE",
                  "source_id": `${id}`,
                  "source_ref_id": `${houseId}`,
                }
              ]
            };
  
      return this.mutate<PastWorkGalleryhoseSecUpdateOPData>(Query, variables); 
    }

    upsertPastWorkGallery(GalleryINData: PastWorkGallery):Promise<PastWorkGalleryUpsertOPData |null | undefined>{
      const Query =`mutation PastWorkGalleryUpsert($input: [PastWorkGalleryUpsertInput!]!) {
                    PastWorkGalleryUpsert(input: $input) {
                      id
                      housesec_id
                      file_image
                      file_image_url {
                        direct
                        thumb
                      }
                      cover
                      created
                      updated
                      fr_house_section {
                        id
                        title
                      }
                    }
                  }`;
            const variables = {
              "input": [
                {
                  "cover": null,
                  "housesec_id": null,
                  "id": null
                }
              ]
            };
  
      return this.mutate<PastWorkGalleryUpsertOPData>(Query, variables); 
    }

    deletePastWorkGallery(Id: number):Promise<PastWorkGalleryDeleteOPData |null | undefined>{
      const Query =`mutation PastWorkGalleryDelete($input: PastWorkGalleryDeleteInput!) {
                      PastWorkGalleryDelete(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<PastWorkGalleryDeleteOPData>(Query, variables); 
    }

    PastWorkGalleryFileUpload(PastWorkGallaryUploadIPData: PastWorkGalleryUploadIPData):Promise<PastWorkGalleryUploadOPData | null | undefined> {
      const Query =`
           mutation PastWorkGalleryUpload($attachment: [Upload!]!, $input: PastWorkGalleryUploadInput!) {
            PastWorkGalleryUpload(attachment: $attachment, input: $input) {
              access_url {
                direct
                thumb
              }
              file_name
              id
              ref_id
              file_field
            }
          }`;
  
          const variables = {
              attachment: PastWorkGallaryUploadIPData.attachment,
              input: PastWorkGallaryUploadIPData.input
          }
      return this.mutateFileUpload(Query, variables); 
    }

    PastWorkGalleryUploadDelete(ref_id:string, id:number):Promise<PastWorkGalleryDelOPData | null | undefined> {
        const Query =`mutation PastWorkGalleryUploadDelete($input: [PastWorkGalleryUploadDeleteInput!]!) {
                      PastWorkGalleryUploadDelete(input: $input) {
                        affected
                        id
                        ref_id
                        file_field
                        snapshot {
                          message
                          result
                          imp
                          mismatch
                          notFound
                          conflict
                          success
                          error
                          alert
                          warning
                          notice
                          info
                        }
                      }
                    }`;

        const variables = {
          "input": [
            {
              "file_field": "FILE_IMAGE",
              "ref_id": `${ref_id}`,
              "id": `${id}`,
            }
          ]
      }
      return this.mutateFileUpload(Query, variables);
    }

    getPastWorkGallerybyHomeSection():Promise<PastWorkGalleryByHomeSection | null | undefined> {
        const variables = {
          "filter": {
              "groupBy": {
                "housesec_id": true
              },
              "where": [
                {
                  "cover": {
                    "nulls": false
                  }
                }
              ],
              "order": {
                "id":"DESC"
              }
            }
      }
      return this.execute(this.PastWorkGalleryQuery, variables);
    }

    getPastWorkGallerybyHomeSectionwithPagination(pageSize:number, start:number):Promise<PastWorkGalleryByHomeSection | null | undefined> {
      const variables = {
        "filter": {
            "groupBy": {
              "housesec_id": true
            },
            "where": [
              {
                "cover": {
                  "nulls": false
                }
              }
            ],
            "order": {
              "id":"DESC"
            },
            "skip": start,
            "take": pageSize
          }
    }
    return this.execute(this.PastWorkGalleryQuery, variables);
  }

    getPastWorkGalleryybyHomeSectionId(housesec_id:number, pageSize:number, start:number):Promise<PastWorkGalleryByHomeSection | null | undefined> {
        const variables = {
          "filter": {
            "where": [
              {
                "housesec_id": {
                  "equal": `${housesec_id}`
                }
              } 
            ],
            "order": {
              "id":"DESC"
            },
            "skip": start,
            "take": pageSize
          }
        }
      return this.execute(this.PastWorkGalleryQuery, variables);
    }

    sortAscGetHPastWorkGalleryybyHomeSection(sortOrder:string,pageSize:number, start:number):Promise<PastWorkGalleryOPData |null | undefined>{
      const Query =`query Rows($filter: PastWorkGalleryFindInput!) {
                      PastWorkGalleryFind(filter: $filter) {
                        rows {
                          id
                          housesec_id
                          file_image
                          file_image_url {
                            direct
                            thumb
                          }
                          cover
                          fr_house_section {
                            id
                            title
                          }
                        }
                      }
                    }`;
      const variables = {
        "filter": {
          "order": {
            "housesec_id": sortOrder
          },
          "skip": start,
          "take": pageSize
        }
    };

      return this.execute(Query, variables); 
    }

}

const pastworkGalleryServiceInstance = new PastWorkGalleryService();
export default pastworkGalleryServiceInstance;