import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
import { ModuleSwitcher } from '../models/app-switcher.enum';
import { Store } from '@ngxs/store';
import { AppSwitchModule } from '../store/actions/app.actions';
import { Router } from '@angular/router';
import { Process } from '../models/process.model';
import { BranchProcess } from '../models/branch-process.model';
import { FILIALE_LANDING_PAGE } from '../utils/app.constants';
import { InitRemissionState, SetRemissionFinishedProcessStatus } from '../store/actions/remission.actions';
import { RemissionFinishingProcessStatus } from '../../modules/remission/models/remission-finishing-process-status.enum';
import { ResetBreadcrumbsTo } from '../store/actions/breadcrumb.actions';
import { Breadcrumb } from '../models/breadcrumb.model';
import { SetBranchProcessCurrentPath } from '../store/actions/branch-process.actions';

@Injectable({
  providedIn: 'root',
})
export class ModuleSwitcherService {
  moduleSwitcher$ = new Subject<ModuleSwitcher>();
  constructor(private store: Store, private router: Router) {}

  switch(_module: ModuleSwitcher, currentProcess: BranchProcess | Process, initBranchBreadcrumbs = false) {
    switch (_module) {
      case ModuleSwitcher.Customer:
        this.customerModuleAction(currentProcess as Process);
        break;
      case ModuleSwitcher.Branch:
        if (initBranchBreadcrumbs) {
          this.initialRemissionBreadcrumbSet();
        }
        this.branchModuleAction(currentProcess as BranchProcess);
        break;
    }
    // this.moduleSwitcher$.next(_module);
  }

  customerModuleAction(currentProcess: Process) {
    this.moduleSwitcher$.next(ModuleSwitcher.Branch);
    let commands = ['/dashboard'],
      extras = {};
    if (currentProcess && currentProcess.currentRoute) {
      commands = [currentProcess.currentRoute];
      if (currentProcess.currentRouteQueryParams) {
        extras = { queryParams: currentProcess.currentRouteQueryParams };
      }
    }
    this.router.navigate(commands, extras).then((resolve) => {
      if (resolve) {
        this.moduleSwitcher$.next(ModuleSwitcher.Customer);
        this.store.dispatch(new AppSwitchModule(ModuleSwitcher.Customer));
      }
    });
  }

  branchModuleAction(branchProcess: BranchProcess) {
    this.moduleSwitcher$.next(ModuleSwitcher.Branch);
    this.store.dispatch(new AppSwitchModule(ModuleSwitcher.Branch));
    if (branchProcess && branchProcess.currentRoute) {
      if (branchProcess.currentRouteQueryParams) {
        this.router.navigate([branchProcess.currentRoute], { queryParams: branchProcess.currentRouteQueryParams });
      } else {
        this.router.navigate([branchProcess.currentRoute]);
      }
    } else {
      if (FILIALE_LANDING_PAGE === '/remission/create') {
        this.store.dispatch(new InitRemissionState());
        this.store.dispatch(new SetRemissionFinishedProcessStatus(RemissionFinishingProcessStatus.notSet));
      }
      this.router.navigate([FILIALE_LANDING_PAGE]);
    }
  }

  switchToBranch() {
    this.store.dispatch(new AppSwitchModule(ModuleSwitcher.Branch));
    this.moduleSwitcher$.next(ModuleSwitcher.Branch);
  }

  switchToCustomer() {
    this.store.dispatch(new AppSwitchModule(ModuleSwitcher.Customer));
    this.moduleSwitcher$.next(ModuleSwitcher.Customer);
  }

  private initialRemissionBreadcrumbSet() {
    const path = '/remission/create';
    this.store.dispatch(
      new ResetBreadcrumbsTo(
        <Breadcrumb>{
          name: 'Remission',
          path: path,
        },
        'remission',
        true,
        true
      )
    );
    this.store.dispatch(new SetBranchProcessCurrentPath(path));
  }
}
