import { Component, OnInit, Input, EventEmitter } from '@angular/core';

import { ActivatedRoute } from '@angular/router';
import { Injector, Type } from '@angular/core';

import { <%-SchemaName%>ListComponent } from './<%-schemaName%>-list.component';
import { <%-SchemaName%>Service } from '../<%-schemaName%>.service';
<%for (let a of associationFor) {
  /* a:
  which schema(lower case), the schema's formal schema name, the association field, 
  the Label text triggering the association, my schemaName (lower case),
  the association field Schema name
  */
  let aField = a[2];
  let aSchemaName = a[5];
  let ASchemaName = a[6];
%>import { <%-ASchemaName%>ListComponent } from '../../<%-aSchemaName%>/<%-aSchemaName%>-list/<%-aSchemaName%>-list.component';
import { <%-ASchemaName%>Service } from '../../<%-aSchemaName%>/<%-aSchemaName%>.service';
<%}%>

@Component({
  <%- include(`/widgets/list/component.template.custom.ts`)%>
})
export class <%-SchemaName%><%-ComponentClassName%>Component extends <%-SchemaName%>ListComponent implements OnInit {
  public override clickItemAction: string = '<%-listViewObj.clickItemAction%>';
  public override cardHasLink: boolean = <%-listViewObj.cardHasLink%>;
  public override cardHasSelect: boolean = <%-listViewObj.cardHasSelect%>;
  public override includeSubDetail: boolean = <%-listViewObj.includeSubDetail%>;
  public override canUpdate: boolean = <%-listViewObj.canUpdate%>;
  public override canDelete: boolean = <%-listViewObj.canDelete%>;
  public override canArchive: boolean = <%-listViewObj.canArchive%>;
  public override canCheck: boolean = <%-listViewObj.canCheck%>;
  public override itemMultiSelect: boolean = <%-listViewObj.itemMultiSelect%>;
  public override majorUi: boolean = <%-listViewObj.majorUi%>;
  
  public parentSchema: string = '';
  public parentItemId: string = '';

  constructor(
      public override <%-schemaName%>Service: <%-SchemaName%>Service,
      public override injector: Injector,
      public override route: ActivatedRoute
  ) {
      super(<%-schemaName%>Service, injector, route);

      this.listViews = [ <%for (let widget of widgetDef.views) {%>'<%-widget%>', <%}%>];
      this.listViewFilter = '<%-widgetDef.views[0]%>';
  }

  override ngOnInit() {
    this.queryOnNgInit = false; // don't do query on the super class.
    super.ngOnInit();

    this.listCategory1 = {}; // no do query based on category;
    this.listCategory2 = {}; // no do query based on category;

    if (!this.associationField) {
      // get from param if not given from the component input
      let association = this.route.snapshot.queryParamMap.get("asso")
      this.associationField = association? association : '';
    }
    <%for (let a of associationFor) {
      /* a:
      which schema(lower case), the schema's formal schema name, the association field, 
      the Label text triggering the association, my schemaName (lower case),
      the association field Schema name, and association field SchemaName
      */
    
      let aField = a[2];
      let aSchemaName = a[5];
      let ASchemaName = a[6];
    %>if ( this.associationField === '<%-aField%>') {
      let <%-aSchemaName%>Svc = this.injector.get<<%-ASchemaName%>Service>(
        <%-ASchemaName%>Service as Type<<%-ASchemaName%>Service>
      );
      this.associationCompInstance = new <%-ASchemaName%>ListComponent(<%-aSchemaName%>Svc, this.injector, this.route);
    }
    <%}%>

    if (this.associationCompInstance) {
      this.associationCompFields = this.associationCompInstance.briefFieldsInfo;
    }

    let ref = this.getParentRouteRefField();
    this.parentSchema = this.referenceFieldsReverseMap[ref];

    this.ignoreField = ref; // used for export (send to server)

    this.parentItemId = this.getParentRouteItemId();
    let id = this.parentItemId;
    if (this.arrayFields.some(x=>x[0] == ref)) {
        this.detail[ref] = {'selection':[{'_id': id}] }; //search on array list
    } else {
        this.detail[ref] = {'_id': id }; //make this as the search context
    }
      
    this.searchList().subscribe((done) => {
      if (done) {
        this.associationCompObjects = [];
        for (let item of this.originalList) {
          const o = item[this.associationField];
          //always put to an array
          let oArray = [];
          if (Array.isArray(o)) {
            for (let oo of o) {
              oArray.push(this.associationCompInstance.formatDetail(oo));
            }
          } else if (typeof o === 'object') {
            oArray.push(this.associationCompInstance.formatDetail(o));
          } else {
            oArray.push({}); // empty object
          }
          this.associationCompObjects.push(oArray);
        }
      }
    });
  }
}
