import { Injectable, Logger } from "@nestjs/common";
import { catchError, Observable, of } from "rxjs";
import { FisCreateMessageUtility } from "../CommonMicroserviceModule/message/FISAppMessageJSUtility/services/fis.create.message";

import { ClientManager } from "../CommonMicroserviceModule/clients/QueryClientModule/ClientManager";

@Injectable()
export class SubscriberProxy {

  static ResponseService: FisCreateMessageUtility;
  static QueryService: FisCreateMessageUtility;

  static tempData: any;

  public className: string;
  public filter: string;
  public returnFormatType: string;
  public requestID: string;

  static responseMessage: string;

  static clientManager: ClientManager = new ClientManager("ApolloClient");


  static getResult() {
    return SubscriberProxy.tempData;
  }

  static subscribeCommonServiceChannel(http, request:any): Observable<any> {
    // let result = {};
    return new Observable((subscriber) => {


      this.clientManager.sendQuery(request.data.payload).subscribe({
          next: (x)=>{
            subscriber.next(x);
          },
          error: (e)=>{
            subscriber.error(e)
          },
          complete: ()=>{
            subscriber.complete();
          }
        })

        // this.client.query({
        //   query: gql`${request.data.payload}`,
        //  }).then(x=>{    
        //    console.log(x.data)   
        //     Object.keys(x.data).forEach(y=>{
        //       Object.keys(x.data[y]).forEach(z=>{
        //         if(z!='__typename')
        //         {
        //           subscriber.next(x.data[y][z]);
        //         }
        //       })
        //     })
        //     subscriber.complete();
        //  }).catch(error=>{
        //   subscriber.error("error subscribe graphql "+ error)
        //  });


      // const obs = http.post(`${process.env.GRAPHQL_HOST}/graphql`,{ query: `${request.data.payload}`})
      // obs.subscribe({
      //   next: (x)=>{
          
            
      //       Object.keys(x.data.data).forEach(y=>{
      //         Object.keys(x.data.data[y]).forEach(z=>{
      //           subscriber.next(x.data.data[y][z])
               
      //         })
      //       })
          
      //     subscriber.complete();
      //   },
      //   error: (e)=>{
      //    subscriber.error("error subscribe graphql "+ e)
      //   },
      //   completed: ()=>{
      //     subscriber.complete();
      //   }
      // })


      // const obs = http.post(`${process.env.GRAPHQL_HOST}/graphql`,{ query: `query q1{
      //   sites{
      //     db
      //   }
      // }`})
      // obs.subscribe({
      //   next: (x)=>{
      //     subscriber.next(x.data)
      //   },
      //   error: (e)=>{
      //    subscriber.error("error subscribe graphql "+ e)
      //   },
      //   completed: ()=>{
      //     subscriber.complete();
      //   }
      // })




    //     // resolve("here")
  
    //   http.post(process.env.SUBSCRIBER_HOST, {
    //     data: request.data
    //   }).subscribe({
    //     next: (x) => {
    //       result = x.data;
    //     },
    //     error: (e) => {
    //       reject(e)
    //     },
    //     complete: () => {
    //       resolve(result)
    //     }
    //   });
    })

  }

}

