import GraphQLService from './GraphQLService';

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

export interface GalleryInspirationOPData {
  InspirationGalleryFind?: {
        rows?:GalleryInspiration[]      
        total : number
        pages : number 
    };
}

export interface GalleryInspirationByIdOPData {
  InspirationGalleryFindOneById?: GalleryInspiration
}

interface GalleryInspirationCreateOPData{
  InspirationGalleryMarkAsMain?: GalleryinspirationInsert
}
interface GalleryInspirationUpdateOPData{
  InspirationGalleryUpdate?: {
    snapshot:SnapshotData
    affected:number
    affectedRows:GalleryInspiration[]  
  }
}
interface GalleryInspirationUpsertOPData{
  InspirationGalleryUpsert?: GalleryInspiration
}
interface GalleryInspirationDeleteOPData {
  InspirationGalleryDelete?:{
    snapshot: SnapshotData
    affected : number
  }
}

export interface InspirationGallaryUploadIPData{
  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 InspirationGallaryUploadOPData{
  InspirationGalleryUpload?: fileUpload[]
}

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

export interface InspirationGallaryByHomeSection{
  InspirationGalleryFind?:{
    rows?:GalleryInspiration[]
    total : number
    pages : number 
  }
}

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

  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 GalleryinspirationInsert{
    affected:number,
    id:number,
    ref_group_relation_field_value:number,
    mark_as_main_field:string,
    snapshot:SnapshotData
  }

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

  interface PastWorkGalleryhoseSecUpdateOPData{
    InspirationGalleryFileRelocation:InspirationGalleryFileRelocationItem[]
  }

  interface InspirationGalleryFileRelocationInput {
    id?: string | null;
    file_name?: string | null;
    ref_id: string;
    affected: number;
    housesec_id:number;

  }

class InspirationGalleryService extends GraphQLService {

    constructor() {
      super();
    }

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

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

    getInspirationGalleryById(Id: number):Promise<GalleryInspirationByIdOPData |null | undefined>{
      const Query =`query InspirationGalleryFindOneById($input: InspirationGalleryFindOneByIdInput!) {
                    InspirationGalleryFindOneById(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); 
    }

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

  updateInspirationGallery(GalleryInspiration:GalleryInspiration, id:number):Promise<GalleryInspirationUpdateOPData |null | undefined>{
    const Query =`mutation InspirationGalleryUpdate($input: InspirationGalleryUpdateInput!) {
                  InspirationGalleryUpdate(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": GalleryInspiration.cover,
                },
                "where": [
                  {
                    "id": {
                      "equal": `${id}`
                    }
                  }
                ]
            }
          };

    return this.mutate<GalleryInspirationUpdateOPData>(Query, variables); 
  }

  updateInsirationGalleryHouseSection(PastWorkGalleryHsecINData: InspirationGalleryFileRelocationInput , id:number,houseid:number):Promise<PastWorkGalleryhoseSecUpdateOPData |null | undefined>{
    const Query =`mutation InspirationGalleryFileRelocation($input: [InspirationGalleryFileRelocationInput!]!) {
                    InspirationGalleryFileRelocation(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); 
  }

  upsertInspirationGallery(GalleryINData: GalleryInspiration):Promise<GalleryInspirationUpsertOPData |null | undefined>{
    const Query =`mutation InspirationGalleryUpsert($input: [InspirationGalleryUpsertInput!]!) {
                    InspirationGalleryUpsert(input: $input) {
                      id
                      housesec_id
                      file_image
                      file_image_url {
                        direct
                        thumb
                      }
                      cover
                      created
                      updated
                      fr_house_section {
                        id
                        title
                      }
                    }
                  }`;
          const variables = {
            "input": [
              {
                "cover": GalleryINData.cover,
                "housesec_id": GalleryINData.housesec_id,
                "id": GalleryINData.id
              }
            ]
          };

    return this.mutate<GalleryInspirationUpsertOPData>(Query, variables); 
  }

  deleteInspirationGallery(Id: number):Promise<GalleryInspirationDeleteOPData |null | undefined>{
    const Query =`mutation InspirationGalleryDelete($input: InspirationGalleryDeleteInput!) {
                    InspirationGalleryDelete(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<GalleryInspirationDeleteOPData>(Query, variables); 
  }

  InspirationGalleryFileUpload(InspirationGallaryUploadIPData:InspirationGallaryUploadIPData):Promise<InspirationGallaryUploadOPData | null | undefined> {
    const Query =`
          mutation InspirationGalleryUpload($attachment: [Upload!]!, $input: InspirationGalleryUploadInput!) {
          InspirationGalleryUpload(attachment: $attachment, input: $input) {
            access_url {
              direct
              thumb
            }
            file_name
            id
            ref_id
            file_field
          }
        }`;

        const variables = {
            attachment: InspirationGallaryUploadIPData.attachment,
            input: InspirationGallaryUploadIPData.input
        }
    return this.mutateFileUpload(Query, variables); 
  }

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

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

  getInspirationGallerybyHomeSection(pageSize:number, start:number):Promise<InspirationGallaryByHomeSection | null | undefined> {

      const variables = {
       "filter": {
          "groupBy": {
            "housesec_id": true
          },
          "where": [
            {
              "cover": {
                "nulls": false
              }
            }
          ],
          "order": {
            "id":"DESC"
          },
          "skip": start,
          "take": pageSize
        }
    }
    return this.execute(this.InspirationGalleryQuery, variables);
  }

  getInspirationGallerybyHomeSectionId(housesec_id:number, pageSize:number, start:number):Promise<InspirationGallaryByHomeSection | null | undefined> {
      const variables = {
        "filter": {
          "where": [
            {
              "housesec_id": {
                "equal": `${housesec_id}`
              }
            }
          ],
          "order": {
            "id":"DESC"
          },
          "skip": start,
          "take": pageSize
        }
      }
    return this.execute(this.InspirationGalleryQuery, variables);
  }
  sortAscInspirationGallery(sortOrder:string,pageSize:number, start:number):Promise<GalleryInspirationOPData |null | undefined>{
    const Query =`query Rows($filter: InspirationGalleryFindInput!) {
                    InspirationGalleryFind(filter: $filter) {
                      rows {
                        id
                        housesec_id
                        file_image
                        file_image_url {
                          thumb
                          direct
                        }
                        cover
                        fr_house_section {
                          id
                          title
                        }
                      }
                      total
                      pages
                    }
                  }`;
                  const variables = {
                    "filter": {
                      "order": {
                        "housesec_id":sortOrder
                      },
                      "skip": start,
                      "take": pageSize
                    }
                };

    return this.execute(Query, variables); 
  }

}

const inspirationGalleryServiceInstance = new InspirationGalleryService();
export default inspirationGalleryServiceInstance;