import GraphQLService from './GraphQLService';

export interface FloorPlanAttachmentData{
  id? : number|string
  floorplan_id : string | number
  file_attachment : string
  cover?:string
  file_attachment_url:{
    direct:string
    secure:string
    thumb:string
  },
  created:string
  updated:string
  
}
export interface FloorPlanAttachmentOPData {
  FloorPlanAttachmentsFind?: {
      rows?:FloorPlanAttachmentData[]      
      total : number
      pages : number 
  };
}
export interface FloorPlanAttachmentIdOPData {
  FloorPlanAttachmentsFindOneById?:FloorPlanAttachmentData
}
export interface FloorPlanAttachmentCreateOPData {
  FloorPlanAttachmentsCreate?:FloorPlanAttachmentData
}

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 FloorPlanAttachmentUpdateOPData {
  FloorPlanAttachmentsUpdate?:{
    snapshot: SnapshotData
    affected : number
    affectedRows ?: FloorPlanAttachmentData
  }
}
export interface FloorPlanAttachmentUpsertOPData {
  FloorPlanAttachmentsUpsert?:FloorPlanAttachmentData
}
export interface FloorPlanAttachmentDeleteOPData {
  FloorPlanAttachmentsDelete?:{
    snapshot: SnapshotData
    affected : number
  }
}

export interface FloorPlanAttachmentsUploadIPData{
  attachmentImg?: File[] | undefined,
  attachmentPDF?:  File[] | undefined,
  input:{
    ref_id : string
    file_field : string
    id?:string | number
  }
}


export interface PFloorPlanAttachmentsUploadOPData{
  FloorPlanAttachmentsUpload?: FloorPlanAttachmentsUploadResponse[]
}

export interface FloorPlanAttOPData {
  FloorPlanAttachmentsFind?: {
      rows?:{
        id : number
        floorplan_id : number
        file_attachment : string
        file_attachment_url?: 
        {
          direct : string
          thumb : string
        }
        cover : string
      }[]
  };
}

export interface FloorPlanAttachmentsUploadResponse {
    file_field: string; 
    id: string;      
    ref_id: string;  
    file_name: string; 
    access_url: {
      direct: string; 
      thumb: string;  
    };
}

interface FloorPlanAttachmentMain{
  id:number,
  ref_group_relation_field_value:number,
  mark_as_main_field:string,
  }


  interface PastWorkFloorPlanAttachmentInsert{
    affected:number,
    id:number,
    ref_group_relation_field_value:number,
    mark_as_main_field:string,
    snapshot:SnapshotData
  }
  interface CreateFloorPlanAttachmentOPData{
    FloorPlanAttachmentsMarkAsMain: PastWorkFloorPlanAttachmentInsert
  }

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

    getFloorPlanAttachment(pageSize:number, start:number):Promise<FloorPlanAttachmentOPData |null | undefined>{
      const Query =`query Rows($filter: FloorPlanAttachmentsFindInput!) {
                    FloorPlanAttachmentsFind(filter: $filter) {
                      rows {
                        id
                        floorplan_id
                        file_attachment
                        type      
                      }
                      total
                      pages
                    }
                  }`;
      const variables = {
          "filter": {
            "order": {
              "id": "DESC"
            },
            "skip": start,
            "take": pageSize
          }
      };
  
      return this.execute(Query, variables); 
    }

    getFloorPlanAttachmentById(Id: number):Promise<FloorPlanAttachmentIdOPData |null | undefined>{
      const Query =`query FloorPlanAttachmentsFindOneById($input: FloorPlanAttachmentsFindOneByIdInput!) {
                      FloorPlanAttachmentsFindOneById(input: $input) {
                        id
                        floorplan_id
                        file_attachment
                        file_attachment_url {
                          direct
                          thumb
                        }
                        type
                      }
                    }`;
      const variables = {
          "input": {
            "id": Id
          }
      };
  
      return this.execute(Query, variables); 
    }

    createFloorPlanAttachment(FPId:number,floorPlanID:number):Promise<CreateFloorPlanAttachmentOPData |null | undefined>{
      const Query =`mutation FloorPlanAttachmentsMarkAsMain($input: [FloorPlanAttachmentsMarkAsMainInput!]!) {
                    FloorPlanAttachmentsMarkAsMain(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": `${FPId}`,
                      "ref_group_relation_field_value": `${floorPlanID}`,
                      "mark_as_main_field": "COVER"
                    }
                };
  
      return this.mutate<CreateFloorPlanAttachmentOPData>(Query, variables); 
    }

    updateFloorPlanAttachment(attachID:number,FPID:number):Promise<FloorPlanAttachmentUpdateOPData |null | undefined>{
      const Query =`mutation FloorPlanAttachmentsUpdate($input: FloorPlanAttachmentsUpdateInput!) {
                      FloorPlanAttachmentsUpdate(input: $input) {
                        snapshot {
                        message
                        result
                        imp
                        mismatch
                        notFound
                        conflict
                        success
                        error
                        alert
                        warning
                        notice
                        info
                      }
                        affectedRows {
                          id
                          floorplan_id
                          file_attachment
                          file_attachment_url {
                            direct
                            secure
                            thumb
                          }
                          cover
                          created
                          updated
                        }
                        affected
                      }
                    }`;
            const variables = {
                "input": {
                  "sets": {
                    "cover": ""
                  },
                  "where": [
                    {
                      "floorplan_id": {
                        "equal": `${FPID}`
                      },
                      "id": {
                        "equal": `${attachID}`
                      }
                    }
                  ]
                }
            };
  
      return this.mutate<FloorPlanAttachmentUpdateOPData>(Query, variables); 
    }

    upsertFloorPlanAttachment(FloorPlanAttachmentINData: FloorPlanAttachmentData):Promise<FloorPlanAttachmentUpsertOPData |null | undefined>{
      const Query =`mutation FloorPlanAttachmentsUpsert($input: [FloorPlanAttachmentsUpsertInput!]!) {
                    FloorPlanAttachmentsUpsert(input: $input) {
                      id
                      floorplan_id
                      file_attachment
                      file_attachment_url {
                        direct
                        thumb
                      }
                      type
                    }
                  }`;
            const variables = {
              input:[{
                "type": null,
                "id": null,
                "floorplan_id": null
              }]
            };
  
      return this.mutate<FloorPlanAttachmentUpsertOPData>(Query, variables); 
    }

    deleteFloorPlanAttachment(Id: number):Promise<FloorPlanAttachmentDeleteOPData |null | undefined>{
      const Query =`mutation FloorPlanAttachmentsDelete($input: FloorPlanAttachmentsDeleteInput!) {
                    FloorPlanAttachmentsDelete(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<FloorPlanAttachmentDeleteOPData>(Query, variables); 
    }

    UploadFloorPlanAttachmentImg(FpAttachmentsUploadIPData: FloorPlanAttachmentsUploadIPData):Promise<PFloorPlanAttachmentsUploadOPData | null | undefined> {
      const Query =`
          mutation FloorPlanAttachmentsUpload($attachment: [Upload!]!, $input: FloorPlanAttachmentsUploadInput!) {
            FloorPlanAttachmentsUpload(attachment: $attachment, input: $input) {
              file_field
              id
              ref_id
              file_name
              access_url {
                direct
                thumb
              }
            }
          }`;
  
          const variables = {
              attachment: FpAttachmentsUploadIPData.attachmentImg,
              input: FpAttachmentsUploadIPData.input,
          }
          
      return this.mutateFileUpload(Query, variables); 
    }

    UploadFloorPlanAttachmentPDF(FpAttachmentsUploadIPData: FloorPlanAttachmentsUploadIPData):Promise<PFloorPlanAttachmentsUploadOPData | null | undefined> {
      const Query =`
          mutation FloorPlanUpload($input: FloorPlanUploadInput!, $attachment: [Upload!]!) {
            FloorPlanUpload(input: $input, attachment: $attachment) {
              file_field
              id
              ref_id
              file_name
              access_url {
                direct
                secure
                thumb
              }
            }
          }`;
  
          const variables = {
              attachment: FpAttachmentsUploadIPData.attachmentPDF,
              input: FpAttachmentsUploadIPData.input
          }
      return this.mutateFileUpload(Query, variables); 
    }

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

    getFloorPlanAttachmentByFloorPlanId(floorPlanId : number):Promise<FloorPlanAttOPData |null | undefined>{
      const Query =`query Rows($filter: FloorPlanAttachmentsFindInput!) {
                    FloorPlanAttachmentsFind(filter: $filter) {
                      rows {
                        id
                        floorplan_id
                        file_attachment
                        file_attachment_url {
                          direct
                          thumb
                        }
                        cover
                      }
                    }
                  }`;
      const variables = {
          "filter": {
            "order": {
              "cover": "DESC",
              "id":"ASC"
            },
            "where": [
              {
                "floorplan_id": {
                  "equal": `${floorPlanId}`
                }
              }
            ]
          }
      };
  
      return this.execute(Query, variables); 
    }

}

const FloorPlanAttachmentServiceInstance = new FloorPlanAttachmentService();
export default FloorPlanAttachmentServiceInstance;