import { Component, OnInit } from '@angular/core';
import { ModalService } from '@libs/ui';
import { Router } from '@angular/router';
import { Store } from '@ngxs/store';

import { AppService } from '@sales/core-services';

import { ModuleSwitcherService } from 'apps/sales/src/app/core/services/module-switcher.service';
import { ResetBreadcrumbsTo } from 'apps/sales/src/app/core/store/actions/breadcrumb.actions';
import { SetBranchProcessCurrentPath } from 'apps/sales/src/app/core/store/actions/branch-process.actions';

@Component({
  selector: 'app-remission-reminder-dialog',
  templateUrl: './remission-reminder-dialog.component.html',
  styleUrls: ['./remission-reminder-dialog.component.scss'],
})
export class RemissionReminderDialogComponent implements OnInit {
  id = 'remission-reminder-modal';
  constructor(
    private appService: AppService,
    private modalService: ModalService,
    private router: Router,
    private store: Store,
    private moduleSwitcher: ModuleSwitcherService
  ) {}

  ngOnInit() {}

  public openDialog() {
    this.modalService.open(this.id);
  }

  closeDialog() {
    this.modalService.close(this.id);
  }

  toRemission() {
    this.closeDialog();
    this.moduleSwitcher.switchToBranch();
    this.navigateRemissionCreateList();
  }

  navigateRemissionCreateList() {
    const path = '/remission/create';
    this.store.dispatch(
      new ResetBreadcrumbsTo(
        {
          path: path,
          name: 'Remission',
        },
        'remission'
      )
    );
    this.appService.clearIdleReminder();
    this.router.navigate([path]);
    this.store.dispatch(new SetBranchProcessCurrentPath(path, true));
  }
}
