import { Component, Input, OnInit, Optional } from '@angular/core';
import { NbDialogRef, NbDialogService, NbToastrService } from '@nebular/theme';
import { first } from 'rxjs/operators';
import { environment } from '../../../environments/environment';
import { HappinessBEST } from '../../entity/HappinessGroup';
import { PastoralDomain } from '../../entity/PastoralDomain';
import { BestService } from '../../services/crudServices/best.service';
import { HappinessGroupService } from '../../services/crudServices/happiness-group.service';
import { LineService, LineGroup } from '../../services/line.service';
import { MsgBoxService } from '../../services/msg-box.service';
import { ADButtons, ADIcon } from '../../ui/alert-dlg/alert-dlg.model';
import { ObjectUtils } from '../../utilities/object-utils';
import { BestEditorDlgComponent } from './best-editor-dlg/best-editor-dlg.component';


@Component({
  selector: 'best-list',
  templateUrl: './best-list-dlg.component.html',
  styleUrls: ['./best-list-dlg.component.scss']
})
export class BestListDlgComponent implements OnInit {

  processing: boolean = false;
  isAdding: boolean = false;
  @Input() group: PastoralDomain;
  datum: HappinessBEST;
  public get isDlg(): boolean {
    return this.dlgRef != null;
  }
  constructor(
    private msgBpxService: MsgBoxService,
    private happinessGroupService: HappinessGroupService,
    private toastrService: NbToastrService,
    private bestService: BestService,
    private lineService: LineService,
    private dlgService: NbDialogService,
    @Optional() private dlgRef: NbDialogRef<BestListDlgComponent>,
  ) { }

  ngOnInit(): void {
    this.group.bests = this.group.bests.sort((a, b) => 0 - (a.name < b.name ? 1 : -1));
  }

  close() {
    this.dlgRef.close();
  }
  addBest() {
    this.msgBpxService.showInputbox('Add Best', 'Please input Best\'s Name').pipe(first()).subscribe(result => {
      if (result) {
        let obj = { groupId: this.group.id, id: '', name: result } as HappinessBEST;
        this.bestService.createOrUpdate(obj).pipe(first()).subscribe(result => {
          this.group.bests.push(result);
        });
      }
    });
  }

  copyBestUrl(best: HappinessBEST) {

    var dummy = document.createElement("textarea");
    document.body.appendChild(dummy);
    dummy.value = `${environment.invitationUrl}${best.id}`;
    dummy.select();
    document.execCommand("copy");
    document.body.removeChild(dummy);
    this.toastrService.success(dummy.value, "複製地址至剪貼簿!");
  }
  copyBestList() {
    let list = "".concat(...this.group.bests.map(b => `${b.name}\t\t: ${environment.invitationUrl}${b.id}\r\n`));
    var dummy = document.createElement("textarea");
    document.body.appendChild(dummy);
    dummy.value = list;
    dummy.select();
    document.execCommand("copy");
    document.body.removeChild(dummy);
    this.toastrService.success(dummy.value, "複製地址至剪貼簿!");

    //this.lineService.pushLineMessage(LineGroup.Chris, list);

  }
  pushListToLine() {
    this.msgBpxService.show('Push Whole List To Line', { text: 'Are You Sure?', icon: ADIcon.QUESTION, buttons: ADButtons.YesNo }).pipe(first()).subscribe(result => {
      if (result) {
        this.lineService.pushCommandMessage(this.group.id, 'BEST');
        this.toastrService.success('Sent', "Sent To Line");
      }
    });
  }
  openEditDlg(best: HappinessBEST) {
    let isAdding = false;
    this.dlgService.open(BestEditorDlgComponent, {
      context: {
        data: isAdding ? null : ObjectUtils.CloneValue(best),
        isAdding: isAdding
      }
    }).onClose.pipe(first()).subscribe(result => {
      if (result) {
        ObjectUtils.CopyValue(result, best);
      }
    });

  }
  downloadBestQRCode(best: HappinessBEST) {

    this.happinessGroupService.getBestQrCode(best.name + ".png", best.id);
  }

}
