import { Component, OnInit, ViewEncapsulation } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { MatDialogRef } from '@angular/material/dialog';
import { fuseAnimations } from '@fuse/animations';
import { FuseAlertType } from '@fuse/components/alert';
import { MemberBankDepositService } from 'app/modules/polyglot/member_bank_deposit/services/member_bank_deposit.service';
import { CreateMemberBankDepositRequest } from 'app/modules/proto/c2se/member_bank_deposit_pb';

@Component({
  selector: 'deposit-compose',
  templateUrl: './deposit-compose.component.html',
  encapsulation: ViewEncapsulation.None,
  animations: fuseAnimations,
})
export class DepositComposeComponent implements OnInit {
  composeForm!: FormGroup;

  alert: { type: FuseAlertType; message: string } = {
    type: 'success',
    message: '입금신청이 완료 되었습니다.',
  };
  showAlert: boolean = false;
  /**
   * Constructor
   */
  constructor(
    public matDialogRef: MatDialogRef<DepositComposeComponent>,
    private _formBuilder: FormBuilder,
    private _memberBankDepositService: MemberBankDepositService
  ) {}

  // -----------------------------------------------------------------------------------------------------
  // @ Lifecycle hooks
  // -----------------------------------------------------------------------------------------------------

  /**
   * On init
   */
  ngOnInit(): void {
    // Create the form
    this.composeForm = this._formBuilder.group({
      accountHolder: ['', [Validators.required]],
      amount: ['', [Validators.required]],
    });
  }

  // -----------------------------------------------------------------------------------------------------
  // @ Public methods
  // -----------------------------------------------------------------------------------------------------

  /**
   * Save and close
   */
  saveAndClose(): void {
    // Save the message as a draft
    this.saveAsDraft();

    // Close the dialog
    this.matDialogRef.close();
  }

  /**
   * Discard the message
   */
  discard(): void {}

  /**
   * Save the message as a draft
   */
  saveAsDraft(): void {}

  /**
   * Send the message
   */
  send(): void {
    if (!this.composeForm.valid) {
      return;
    }

    this.composeForm?.disable();

    const req = new CreateMemberBankDepositRequest();
    const amount = this.composeForm.get('amount')?.value as number;
    const holder = this.composeForm.get('accountHolder')?.value as string;
    req.setAmount(amount);
    req.setName(holder);
    req.setMemo('test');

    this._memberBankDepositService
      .createMemberBankDeposit(req)
      .then(() => {
        console.log();
        this.showAlert = true;
      })
      .catch((e) => {
        this.showAlert = true;
        this.alert = { type: 'error', message: '입금신청이 실패 하였습니다.' };
        // Re-enable the form
        this.composeForm?.enable();

        // Reset the form
      })
      .finally(() =>
        setTimeout(() => {
          this.matDialogRef.close();
        }, 2000)
      );
    // req.set
  }

  __changeAmount(amount: string): void {
    const amountInput = this.composeForm.get('amount');

    if (amount === '0') {
      amountInput?.setValue('');
      return;
    }
    amountInput?.setValue(amount);
  }

  __contactDeposit(): void {
    this.showAlert = true;
    this.alert = {
      type: 'success',
      message: '입금 계좌문의가 전달되었습니다!',
    };

    setTimeout(() => {
      this.showAlert = false;
    }, 5000);
  }
}
