import { Component, Input, OnInit, ComponentFactoryResolver, ViewChild, SimpleChanges, ComponentFactory, Inject, Type} from '@angular/core';
import { IItem } from '@farmmaps/common';
import { AbstractSelectedItemComponent, SelectedItemComponent  } from '../selected-item/selected-item.component';
import { WidgetHostDirective } from '../widget-host/widget-host.directive';
import { IItemLayer } from '../../models/item.layer';


@Component({
    selector: 'fm-map-selected-item-container',
    templateUrl: './selected-item-container.component.html',
    styleUrls: ['./selected-item-container.component.scss'],
    standalone: false
})
export class SelectedItemContainerComponent {

  constructor(private componentFactoryResolver: ComponentFactoryResolver, @Inject(AbstractSelectedItemComponent) public selectedItemComponents: AbstractSelectedItemComponent[] ) {
    this.selectedItemComponents = [...this.selectedItemComponents].reverse();
  }

  @Input() item: IItem;
  @Input() parentItem: IItem;
  @Input() itemLayer:IItemLayer;
  @Input() overlayLayers:Array<IItemLayer>;

  @ViewChild(WidgetHostDirective, { static: true }) widgetHost: WidgetHostDirective;

  loadComponent() {
    let componentFactory: ComponentFactory<AbstractSelectedItemComponent> = this.componentFactoryResolver.resolveComponentFactory(SelectedItemComponent); // default

    let selected = -1;
    let maxMatches =0;
    const showItem = true;
    for (let i = 0; i < this.selectedItemComponents.length; i++) {
      let matches=0;
      let criteria=0;
      if (this.selectedItemComponents[i]['forItemType'] ) {
        criteria++;
        if(this.selectedItemComponents[i]['forItemType'].split(",").filter(part => part ==this.item.itemType).length == 1) {
          matches++;
        }
      }
      if (this.selectedItemComponents[i]['forSourceTask']) {
        criteria++;
        if( this.selectedItemComponents[i]['forSourceTask'].split(",").filter(part => part ==this.item.sourceTask).length == 1) {
          matches++;
        }
      }

      if(criteria==matches && matches > maxMatches) {
        selected=i;
        maxMatches = matches;
      }
    }
    if (selected >= 0) {
      componentFactory = this.componentFactoryResolver.resolveComponentFactory(this.selectedItemComponents[selected]['constructor'] as any);
    }

    const viewContainerRef = this.widgetHost.viewContainerRef;
    viewContainerRef.clear();

    const componentRef = viewContainerRef.createComponent(componentFactory);
    (<AbstractSelectedItemComponent>componentRef.instance).item = this.item;
    (<AbstractSelectedItemComponent>componentRef.instance).parentItem = this.parentItem;
    (<AbstractSelectedItemComponent>componentRef.instance).itemLayer = this.itemLayer;
    (<AbstractSelectedItemComponent>componentRef.instance).overlayLayers = this.overlayLayers;
  }

  ngOnChanges(changes: SimpleChanges) {
    if (changes["item"] && changes["item"].currentValue) {
        this.loadComponent();
    }
    if (changes["itemLayer"] && changes["itemLayer"].currentValue) {
      this.loadComponent();
    }
    if (changes["overlayLayers"] && changes["overlayLayers"].currentValue) {
      this.loadComponent();
    }
  }
}
