import { MatDialog } from '@angular/material/dialog';
import { ObjectUIServiceBase } from '@objects-view/object-ui-service-base';
import { Router } from '@angular/router';
import { ConfigService } from '@shared/config/config.service';
import { IPermissionsModel } from '@fleio-api/base-model/IPermissionsModel';
import { IIcon } from '@shared/ui/common/interfaces/icon';
import { IObjectStatus, StatusType, StatusValue } from '@objects-view/interfaces/object-status';
import { ITitle } from '@objects-view/interfaces/card-data/card-title';
import { IAction } from '@objects-view/interfaces/actions/action';
import { IDataField } from '@objects-view/interfaces/card-data/data-field';
import { IDetailsTab } from '@objects-view/interfaces/details/details-tab';
import { IRevenueReportModel } from '@fleio-api/utils/model/revenue-report.model';
import { RevenueReportsApiService } from '@fleio-api/utils/revenue-reports/revenue-reports-api.service';
import { RevenueReportOverviewComponent } from './tabs/revenue-report-overview/revenue-report-overview.component';
import { ApiCallAction } from '@objects-view/actions/api-call-action';
import { RouterLinkAction } from '@objects-view/actions/router-link-action';
import { CallbackAction } from '@objects-view/actions/callback-action';
import { RevenueReportGenerateFormComponent } from './tabs/revenue-report-generate-form/revenue-report-generate-form.component';
import { AppColor } from '@shared/ui/common/enums/app-color.enum';

export class RevenueReportUiService extends ObjectUIServiceBase<IRevenueReportModel> {
  private readonly router: Router;
  private readonly config: ConfigService;
  private readonly revenueReportsApiService: RevenueReportsApiService;
  private readonly matDialog: MatDialog;


  constructor(
    revenueReports: IRevenueReportModel, permissions: IPermissionsModel, state: string,
    router: Router, config: ConfigService, revenueReportsApiService: RevenueReportsApiService, matDialog: MatDialog,
  ) {
    super(revenueReports, permissions, state);
    this.matDialog = matDialog;
    this.router = router;
    this.config = config;
    this.revenueReportsApiService = revenueReportsApiService;
  }

  getIcon(): IIcon {
    if (!this.object || (this.object && !this.object.id)) {
      return null;
    }
    if (!this.object.generating) {
      return {
        name: 'check',
        color: AppColor.Green
      };
    } else {
      return {
        name: 'close',
        color: AppColor.Red
      };
    }
  }

  getStatus(): IObjectStatus {
    return { type: StatusType.None, value: StatusValue.None };
  }

  getTitle(): ITitle {
    switch (this.state) {
      case 'details':
        return {
          text: $localize`Report for ${this.object.report_month_year}`,
        };
      case 'generate':
        return {
          text: $localize`Generate report`,
        };

      default:
        return {
          text: $localize`Report for ${this.object.report_month_year}`,
        };
    }
  }

  getActions(): IAction[] {
    const actions: IAction[] = [];
    actions.push(new ApiCallAction(
      {
        object: this.object,
        icon: {name: 'redo'},
        name: $localize`Regenerate`,
        tooltip: $localize`Regenerate`,
        confirmOptions: {
          confirm: true,
          title: $localize`Regenerate report?`,
          message: $localize`Are you sure you want to regenerate report?`
        },
        apiService: this.revenueReportsApiService,
        apiAction: 'regenerate_report',
        refreshAfterExecute: false,
        redirectAfterExecute: true,
        redirectUrl: this.config.getPanelUrl('utilities/reports/revenue-reports'),
      }
    ));
    return actions;
  }

  getDetailsLink(): string {
    return this.config.getPanelUrl(`utilities/reports/revenue-reports/${this.object.id}`);
  }

  getCardFields(): IDataField[] {
    return [
    ];
  }

  getCardTags(): string[] {
    return [];
  }

  getTabs(): IDetailsTab[] {
    switch (this.state) {
      case 'details':
        return [
          {
            tabName: $localize`Overview`,
            tabIdentifier: 'overview',
            component: RevenueReportOverviewComponent,
          },
        ];
      case 'generate':
        return [
          {
            tabName: $localize`Generate`,
            tabIdentifier: 'generate',
            component: RevenueReportGenerateFormComponent,
          },
        ];
    }
  }

  getDetailsActions(): IAction[] {
    const actions = [];

    switch (this.state) {
      case 'generate':
        actions.push(new RouterLinkAction({
            name: $localize`Cancel`,
            routerUrl: this.config.getPrevUrl(`utilities/reports/revenue-reports`),
            router: this.router,
          }
        ));
        actions.push(new CallbackAction({name: $localize`Generate`}));
        break;
      default:
        break;
    }

    return actions;
  }

  getObjectDetailsRefreshInterval(): number {
    switch (this.state) {
      case 'details':
        if (this.object && this.object.generating) {
          return this.config?.current?.settings?.refreshIntervals?.reportDetailsInterval;
        } else {
          return this.config.getDefaultRefreshInterval();
        }
      default:
        return null;
    }
  }

}
