import { Component, ElementRef, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { NcApplication, NgCortexService } from '@ng-cortex/core';
import { NzModalService } from 'ng-zorro-antd/modal';

/** 应用索引页面 */
@Component({
  selector: 'nc-application-index',
  templateUrl: './nc-application-index.page.html',
  styleUrls: ['./nc-application-index.page.scss']
})
export class NcApplicationIndexPage implements OnInit {

  /** 应用索引路由 */
  _routeForApplicationIndex!: string;
  /** 应用关键字 */
  _applicationKeyword?: string;
  /** 应用组列表 */
  _applicationGroupList: any[] = [];

  constructor(
    private _elementRef: ElementRef,
    private _router: Router,
    private _nzModalService: NzModalService,
    private _ngCortexService: NgCortexService) { }

  /** 初始化后 */
  ngOnInit(): void {
    const route = this._router.url;
    const element = this._elementRef.nativeElement;
    const { applicationList, currentApplication, routeRecordList } = this._ngCortexService.data;
    if (applicationList.length === 1) {
      this._ngCortexService.closeRoute(route);
    } else {
      if (routeRecordList.length === 0) {
        if (currentApplication) {
          this._ngCortexService.closeCurrentApplication();
        }
      } else {
        element.hidden = true;
        this._nzModalService.confirm({
          nzTitle: '返回首页',
          nzContent: currentApplication
            ? '即将退出 <strong>“' + currentApplication.name + '”</strong> 应用，当前打开页面将被全部关闭。'
            : '当前打开页面将被全部关闭。',
          nzOnOk: () => {
            element.hidden = false;
            if (currentApplication) {
              this._ngCortexService.closeCurrentApplication();
            } else {
              this._ngCortexService.closeAllRoutes();
            }
          },
          nzOnCancel: () => {
            this._ngCortexService.closeRoute(route);
          }
        });
      }
    }
    this._routeForApplicationIndex = this._ngCortexService.routeForApplicationIndex;
    this._generateApplicationGroupList(applicationList);
  }

  /** 搜索应用列表 */
  _searchApplicationList(): void {
    let { applicationList } = this._ngCortexService.data;
    const keyword = this._applicationKeyword && this._applicationKeyword.trim();
    if (keyword) {
      applicationList = applicationList.filter(application => {
        const name = application.name || '';
        const code = application.code || '';
        return name.includes(keyword) || code.includes(keyword);
      });
    }
    this._generateApplicationGroupList(applicationList);
  }

  /**
   * 生成应用组列表
   * @param applicationList 应用列表
   */
  private _generateApplicationGroupList(applicationList: NcApplication[]): void {
    const groupMap = new Map();
    applicationList.forEach(application => {
      const groupName = application.groupName || `其它`;
      let group = groupMap.get(groupName);
      if (!group) {
        group = { name: groupName, children: [] };
        groupMap.set(groupName, group);
      }
      group.children.push(application);
    });
    this._applicationGroupList = Array.from(groupMap.values());
  }

}
