import { CdkDragDrop, moveItemInArray } from '@angular/cdk/drag-drop';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { AfterViewInit, Component, OnInit, TemplateRef, ViewChild } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { Router } from '@angular/router';
import { NcActionStatus, NcTableField } from '@ng-cortex/core';
import { getErrorMessage, isValid, validateForm } from '@ng-cortex/utils';
import { NzNotificationService } from 'ng-zorro-antd/notification';
import { Observable, of } from 'rxjs';
import { catchError, map, mergeMap, tap } from 'rxjs/operators';
import { environment } from '../../../../environments/environment';

/** 系统消息列表页面 */
@Component({
  selector: 'nc-system-message',
  templateUrl: './nc-system-message.page.html',
  styleUrls: ['./nc-system-message.page.less']
})
export class NcSystemMessagePage implements OnInit, AfterViewInit {

  /** 标题内容模板 */
  @ViewChild('titleTemplate') _titleTemplate!: TemplateRef<{ $implicit: any; source: any; }>;
  /** 时间内容模板 */
  @ViewChild('timeTemplate') _timeTemplate!: TemplateRef<{ $implicit: string; }>;
  /** 是否已读头部模板 */
  @ViewChild('readHeadTemplate') _readHeadTemplate!: TemplateRef<any>;
  /** 是否已读主体模板 */
  @ViewChild('readBodyTemplate') _readBodyTemplate!: TemplateRef<{ $implicit: boolean; }>;
  /** 发送人内容模板 */
  @ViewChild('senderBodyTemplate') _senderBodyTemplate!: TemplateRef<{ extra: any; }>;

  /** 系统消息明细路由 */
  _systemMessageDetailRoute!: string;
  /** 系统消息列表 */
  _systemMessageList: any[] = [];
  /** 系统消息额外属性 */
  _systemMessageExtras: { [id: string]: any; } = {};
  /** 系统消息总数 */
  _systemMessageTotal: number = 0;
  /** 系统消息搜索表单 */
  _systemMessageSearchForm!: FormGroup;
  /** 系统消息参数 */
  _systemMessageParams = {
    page: 1,
    size: 20,
    sort: 'id,desc'
  };
  /** 系统消息加载状态 */
  _systemMessageLoading: boolean = false;
  /** 系统消息全部选中状态 */
  _systemMessageAllChecked: boolean = false;
  /** 系统消息模糊选中状态 */
  _systemMessageIndeterminate: boolean = false;
  /** 系统消息字段列表 */
  _systemMessageFieldList: NcTableField[] = [
    { key: 'read', name: '', visible: true, width: '40px', align: 'center' },
    { key: 'title', name: '标题', visible: true, width: '320px', sortKey: 'title' },
    { key: 'content', name: '内容', visible: true, width: '560px', sortKey: 'content' },
    { key: 'sender', name: '发送人', visible: true, width: '120px', sortKey: 'sender' },
    { key: 'sendTime', name: '发送时间', visible: true, width: '160px', sortKey: 'sendTime' }
  ];
  /** 标记系统消息已读状态 */
  _markSystemMessageAsReadStatus: NcActionStatus = false;
  /** 标记系统消息已读加载状态 */
  _markSystemMessageAsReadLoading: boolean = false;

  /** 取消获取系统消息列表 */
  private _cancelGetSystemMessageListFn = () => { };

  constructor(
    private _formBuilder: FormBuilder,
    private _httpClient: HttpClient,
    private _router: Router,
    private _nzNotificationService: NzNotificationService) { }

  /** 初始化后 */
  ngOnInit(): void {
    this._systemMessageDetailRoute = this._router.url.replace(/\?.*$/, '') + '-detail';
    this._systemMessageFieldList.forEach((field, index) => {
      field.defaultSequence = index + 1;
      field.defaultVisible = field.visible;
      field.defaultWidth = field.width;
    });
    this._systemMessageSearchForm = this._formBuilder.group({
      keywords: [null],
      sendTime: [null],
      read: [null]
    });
    validateForm(this._systemMessageSearchForm, true).subscribe(() => this._getSystemMessageList().subscribe());
    this._updateSystemMessageActionStatus();
  }

  /** 初始次渲染后 */
  ngAfterViewInit(): void {
    requestAnimationFrame(() => {
      this._systemMessageFieldList.forEach(field => {
        if (/^(title)$/.test(field.key)) {
          field.bodyTemplate = this._titleTemplate;
        }
        if (/^(sendTime)$/.test(field.key)) {
          field.bodyTemplate = this._timeTemplate;
        }
        if (/^(read)$/.test(field.key)) {
          field.headTemplate = this._readHeadTemplate;
          field.bodyTemplate = this._readBodyTemplate;
        }
        if (/^(sender)$/.test(field.key)) {
          field.bodyTemplate = this._senderBodyTemplate;
        }
      });
    });
  }

  /** 查询系统消息列表 */
  _querySystemMessageList(): void {
    validateForm(this._systemMessageSearchForm).subscribe(() => this._getSystemMessageList().subscribe());
  }

  /** 重新查询系统消息列表 */
  _requerySystemMessageList(): void {
    this._systemMessageParams.page = 1;
    this._querySystemMessageList();
  }

  /** 重置系统消息搜索表单 */
  _resetSystemMessageSearchForm(): void {
    this._systemMessageSearchForm.reset();
    this._querySystemMessageList();
  }

  /**
   * 拖拽系统消息字段列表
   * @param event 拖拽事件
   */
  _dropSystemMessageFieldList(event: CdkDragDrop<string[]>): void {
    moveItemInArray(this._systemMessageFieldList, event.previousIndex, event.currentIndex);
  }

  /** 重置系统消息字段列表 */
  _resetSystemMessageFieldList(): void {
    this._systemMessageFieldList = this._systemMessageFieldList
      .map(field => {
        field.visible = field.defaultVisible!;
        field.width = field.defaultWidth;
        return field;
      })
      .sort((fieldA, fieldB) => fieldA.defaultSequence! - fieldB.defaultSequence!);
  }

  /**
   * 改变系统消息字段排序
   * @param field 字段
   */
  _changeSystemMessageFieldSort(field: NcTableField): void {
    const targetField = field;
    this._systemMessageFieldList
      .filter(field => field !== targetField && field.sortOrder)
      .forEach(field => field.sortOrder = null);
    if (field.sortOrder) {
      this._systemMessageParams.sort = field.sortKey + ',' + field.sortOrder.slice(0, -3);
    } else {
      this._systemMessageParams.sort = 'id,desc';
    }
    this._requerySystemMessageList();
  }

  /**
   * 选中所有系统消息列表
   * @param checked 选中状态
   */
  _checkAllSystemMessageList(checked: boolean): void {
    for (const id in this._systemMessageExtras) {
      this._systemMessageExtras[id].checked = checked;
    }
    this._systemMessageAllChecked = checked;
    this._systemMessageIndeterminate = false;
    this._updateSystemMessageActionStatus();
  }

  /** 更新系统消息选中状态 */
  _updateSystemMessageCheckStatus(): void {
    const checkedSystemMessageList = this._systemMessageList.filter(systemMessage => this._systemMessageExtras[systemMessage.id].checked);
    switch (checkedSystemMessageList.length) {
      case 0:
        this._systemMessageAllChecked = false;
        this._systemMessageIndeterminate = false;
        break;
      case this._systemMessageList.length:
        this._systemMessageAllChecked = true;
        this._systemMessageIndeterminate = false;
        break;
      default:
        this._systemMessageAllChecked = false;
        this._systemMessageIndeterminate = true;
    }
    this._updateSystemMessageActionStatus();
  }

  /** 标记系统消息已读 */
  _markSystemMessageAsRead(): void {
    this._markSystemMessageAsReadLoading = true;
    const checkedSystemMessageList = this._systemMessageList.filter(systemMessage => this._systemMessageExtras[systemMessage.id].checked);
    const url = '@' + environment.coreService + '/api/system-message-deliveries/mark-as-read';
    const params: any = { ids: checkedSystemMessageList.map(systemMessage => systemMessage.id) };
    this._httpClient.put(url, null, { params: params, responseType: 'text' })
      .pipe(tap(() => this._querySystemMessageList()))
      .pipe(catchError((httpErrorResponse: HttpErrorResponse) => {
        this._nzNotificationService.error('标记已读失败', getErrorMessage(httpErrorResponse));
        return of(undefined);
      }))
      .subscribe(() => {
        this._markSystemMessageAsReadLoading = false;
      });
  }

  /** 更新系统消息已读状态 */
  _updateSystemMessageReadStatus(systemMessage: any): void {
    systemMessage.read = true;
  }

  /** 获取系统消息列表 */
  private _getSystemMessageList(): Observable<HttpErrorResponse | Error | null> {
    this._cancelGetSystemMessageListFn();
    return new Observable(subscriber => {
      if (this._systemMessageSearchForm.valid) {
        this._systemMessageLoading = true;
        const completeGetSystemMessageListFn = () => {
          this._cancelGetSystemMessageListFn = () => { };
          this._systemMessageLoading = false;
        };
        const url = '@' + environment.coreService + '/api/system-message-deliveries';
        const params: any = {
          ...this._systemMessageSearchForm.getRawValue(),
          ...this._systemMessageParams
        };
        for (const key in params) {
          if (!isValid(params[key])) {
            delete params[key];
          }
        }
        if (params.sendTime) {
          params.sendTimeFrom = params.sendTime[0].toISOString();
          params.sendTimeTo = params.sendTime[1].toISOString();
          delete params.sendTime;
        }
        params.page--;
        const getSystemMessageListSubscription = this._httpClient.get<any[]>(url, { params: params, observe: 'response' })
          .pipe(mergeMap(httpResponse => {
            const systemMessageList = httpResponse.body!;
            const systemMessageTotal = Number(httpResponse.headers.get('X-Total-Count'));
            if (systemMessageList.length === 0 && systemMessageTotal > 0) {
              this._systemMessageParams.page = Math.ceil(systemMessageTotal / params.size);
              params.page = this._systemMessageParams.page - 1;
              return this._httpClient.get<any[]>(url, { params: params, observe: 'response' })
                .pipe(map(httpResponse => {
                  const systemMessageList = httpResponse.body!;
                  const systemMessageTotal = Number(httpResponse.headers.get('X-Total-Count'));
                  return { systemMessageList, systemMessageTotal };
                }));
            }
            return of({ systemMessageList, systemMessageTotal });
          }))
          .pipe(tap(() => subscriber.next(undefined)))
          .pipe(catchError((httpErrorResponse: HttpErrorResponse) => {
            this._nzNotificationService.error('获取系统消息列表失败', getErrorMessage(httpErrorResponse));
            subscriber.next(httpErrorResponse);
            return of({ systemMessageList: [], systemMessageTotal: 0 });
          }))
          .subscribe(({ systemMessageList, systemMessageTotal }) => {
            completeGetSystemMessageListFn();
            const systemMessageExtras: { [id: string]: any; } = {};
            systemMessageList.forEach(systemMessage => {
              systemMessageExtras[systemMessage.id] = {
                checked: false,
                sender: JSON.parse(systemMessage.sender) || {}
              };
            });
            this._systemMessageList = systemMessageList;
            this._systemMessageTotal = systemMessageTotal;
            this._systemMessageExtras = systemMessageExtras;
            this._systemMessageAllChecked = false;
            this._systemMessageIndeterminate = false;
            this._updateSystemMessageActionStatus();
            subscriber.complete();
          });
        this._cancelGetSystemMessageListFn = () => {
          getSystemMessageListSubscription.unsubscribe();
          completeGetSystemMessageListFn();
          subscriber.next(new Error('取消获取系统消息列表'));
          subscriber.complete();
        };
      } else {
        subscriber.next(new Error('筛选系统消息不合法'));
        subscriber.complete();
      }
    });
  }

  /** 更新系统消息操作状态 */
  private _updateSystemMessageActionStatus(): void {
    const checkedSystemMessageList = this._systemMessageList.filter(systemMessage => this._systemMessageExtras[systemMessage.id].checked);
    if (checkedSystemMessageList.length > 0) {
      this._markSystemMessageAsReadStatus = true;
    } else {
      this._markSystemMessageAsReadStatus = { disabled: true, hint: '未选中系统消息' };
    }
  }

}
