import { Component, Input, OnInit, SimpleChanges, ViewChild, ViewContainerRef, Output, EventEmitter, ComponentFactoryResolver} from '@angular/core';
import { Store } from '../Store';

@Component({
    selector: 'vis_asst_response_impl',
    templateUrl: './vis_asst_response_impl.component.html',
    styleUrls: ['./vis_asst_response_impl.component.css'],
  })

export class VisAsstResponseImplComponent implements OnInit {

  @Input() message: any;
  @Input() response : any;
  @Input() template : any;
  @Input() context : any;
  messageData = {};
  @ViewChild('visAsstResponse', { read: ViewContainerRef }) visAsstResponse:any;
  // respCols=[];

//   @Input() visualData : any;
  @Output() saveData: EventEmitter<any> = new EventEmitter();
  @Output() openDrillDown: EventEmitter<any> = new EventEmitter();
  @Output() onRowSelect: EventEmitter<any> = new EventEmitter();
//   //Added by nikhil on 08-12-2021 for xsl visual
//   @Output() onSetLayoutData: EventEmitter<any> = new EventEmitter();
//   @ViewChild('visualInsight', { read: ViewContainerRef }) visualInsight:any;
  compRef:any;

  constructor(private _componentFactoryResolver: ComponentFactoryResolver) {}

  ngOnInit()
  {
    console.log('Print message of vision::::', this.message);
    console.log('Print response of vision::::', this.response);
    // for(let i=0;i<this.response.length;i++)
    // {
    //   this.respCols = this.response[0].cols;
    //   console.log('Print respCols ::::',this.respCols);
    // }
    console.log('Print template of vision:::::', this.template);
    console.log('Print context of vision::::', this.context);
  }

  ngAfterViewInit()
  {
    
  }

  ngOnChanges(changes: SimpleChanges)
  {
    console.log('Print changes inside onchange of visual::: ', changes);
    for (const propName in changes)
    // for (const response in changes)
    {
      if (changes.hasOwnProperty(propName)) 
      {
        // switch (propName)
        // {
        //   case 'template' : {
            // this.visualData = changes[propName]['currentValue'];
            console.log('Print visualData line 61:: ',this.message);
            console.log('Print visualData line 62:: ',this.message['format']);
            if(this.message['format'] == '3' || this.message['format'] == '6')
            {
              console.log('Print visualData line 65:: ', this.context);
              // console.log('Print visualData inside visual component 59 ', this.visAsstResponse)
              if(this.context != undefined)
              {
                setTimeout( () => {
                // let className = this.visualData['visualLayout']['ID'];
                // let className = "vis_asst_response_table";
                let formatName = this.message['format'];
                let comp : any = (Store as any)[formatName];
                console.log('Print ngAfterViewInit comp name line 74::: ', comp);
                let compFactory =  this._componentFactoryResolver.resolveComponentFactory(comp)
                console.log('Print compFactory::::::' ,compFactory, this.visAsstResponse);
                this.compRef = this.visAsstResponse.createComponent(compFactory);
                // this.compRef.instance.visualData = this.visualData;
                this.messageData['response'] = this.response;
  
                this.messageData['template'] = this.template;
                this.messageData['context'] = this.context;
  
                let tempVisualData = JSON.parse(JSON.stringify(this.messageData));
                console.log('Print tempVisualData::::::::: ', tempVisualData);
  
                let saveResp = this.compRef.instance.showMessage(tempVisualData);
                console.log('Print saveResp 79:::::::::: ', saveResp);
  
                // if(saveResp)
                // {
                //   this.saveData.emit(saveResp)
                // }
  
                }, 100)
              }
            }
            break;
        //   }
        // }
      }
    }
  }
}