import { Component, Inject, OnInit } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { Override, Page, Pageable, MagI18nPair, MagTableColumn, MagTableColumnBuilder } from 'ng-stalk/core/model';
import {
  CrudAction,
  ActionButton,
  ActionButtonPreset,
  CrudActionResolver,
  CrudModal2Component,
  CrudPage2Component,
  MagGroupAndFilterDescription,
  SelectableZorroDataSource
} from 'ng-stalk/core/crud';
import { ContentAreaImpl2 } from 'ng-stalk/modal-template'
import {
  MagCodeCacheService,
  MagDateService,
  MagDialogService,
  MagFormService,
  MagMatchMediaService,
  MagSpaceService,
  MagSpaceInfo,
  MagSpaceStatus,
} from 'ng-stalk/core/service';
import { CodeAndName} from 'ng-stalk/core/types';
import { MagUtil } from 'ng-stalk/core/util';
import { NzSafeAny } from 'ng-zorro-antd/core/types';
import { NzModalRef, NzModalService } from 'ng-zorro-antd/modal';

import { <%= classify(name) %>Dto } from '../sample/<%= dasherize(name) %>-dto';
import { Helpers } from '../sample/helpers';
import { <%= classify(name) %>Service } from '../service/<%= dasherize(name) %>-service/<%= dasherize(name) %>.service';

@Component({
  selector: 'app-<%= dasherize(name) %>-detail-modal',
  templateUrl: './<%= dasherize(name) %>-detail-modal.component.html',
  styleUrls: ['./<%= dasherize(name) %>-detail-modal.component.less']
})
export class <%= classify(name) %>DetailModalComponent extends CrudModal2Component<<%= classify(name) %>Dto> implements OnInit {

  // --------------------------------------------------------------------------
  // MAG: 모달 공통 속성
  // --------------------------------------------------------------------------

  model: <%= classify(name) %>Dto;
  shareUrl: string;
  actionButtons: ActionButton[] = ActionButtonPreset.detail();

  /* TODO Preset 에 정의된 버튼들외에 임의로 구성하고 싶은 경우 다음과 같이 사용할 수 있다.
  actionButtons: ActionButton[] = [
    // 상태 변경을 유발하지 않을 경우에는 컬러 지정을 하지 않는다.
    new ActionButton(CrudAction.close, "닫기", {
      type: 'button',
    }),
    new ActionButton(CrudAction.openEditModal, "수정하기", {
      type: 'button',
    }),
    new ActionButton(CrudAction.openDeletePrompt, "삭제하기", {
      type: 'button'
    }),

    // 폼 사용 등으로 데이터 변경(상태 변경)이 일어나는 시점에 submit 및 color 지정을 하도록 한다.
    new ActionButton(CrudAction.doAdd, "적용하기", {
      color: 'accent',
      type: 'submit',
    }),
  ];
  */

  // --------------------------------------------------------------------------
  // 어플리케이션 속성
  // --------------------------------------------------------------------------

  constructor(
    public dialogRef: NzModalRef<<%= classify(name) %>DetailModalComponent>,
    public nzModalService: NzModalService,
    private translateService: TranslateService,
    public magDialogService: MagDialogService,
    private <%= name %>Service: <%= classify(name) %>Service,
  ) {
    super(dialogRef);
  }

  // --------------------------------------------------------------------------
  // MAG: 라이프 사이클 및 필수 기능 구현(oInit, makeDialogTitle, onAction, onDestroy)
  // --------------------------------------------------------------------------

  @Override
  ngOnInit(): void {
    super.ngOnInit();
    this.model = this.data.model;
    // this.model = dialogRef.getContentComponent(); //  data.model;
    // this.shareUrl = `https://localhost:8200/<%= dasherize(name) %>/detail/${this.model.id}`;
  }

  @Override
  makeDialogTitle(): string {
    return `<%= displayName %> 조회하기`;
  }

  @Override
  onAction(action: CrudAction | string): void {
    if (action === CrudAction.openEditModal) {
      this.modify();
    } else if (action === CrudAction.openDeletePrompt) {
      this.delete();
    }
  }

  // --------------------------------------------------------------------------
  // 어플리케이션 비즈니스 로직
  // --------------------------------------------------------------------------

  delete(): void {
    this.magDialogService.confirm({
        title: '<%= displayName %> 삭제',
        message: this.translateService.instant('sample.common.prompt_delete')
    }).subscribe((result: boolean) => {
      if (result) {
        this.<%= name %>Service.deleteOne(this.model.id)
          .then(() => {
            super.doModalAction(CrudAction.doDelete, this.model);
          }).catch((error: NzSafeAny) => Helpers.handleError(error));
      }
    });
  }

  modify(): void {
    super.doModalAction(CrudAction.openEditModal, this.model);
  }

}

