import { IPermissionsModel } from '@fleio-api/base-model/IPermissionsModel';
import { IIcon } from '@shared/ui/common/interfaces/icon';
import { ITitle } from '@objects-view/interfaces/card-data/card-title';
import { IObjectStatus, StatusType, StatusValue } from '@objects-view/interfaces/object-status';
import { ObjectUIServiceBase } from '@objects-view/object-ui-service-base';
import { IAction } from '@objects-view/interfaces/actions/action';
import { Router } from '@angular/router';
import { ConfigService } from '@shared/config/config.service';
import { IDataField } from '@objects-view/interfaces/card-data/data-field';
import { IDetailsTab } from '@objects-view/interfaces/details/details-tab';
import { ITaxRuleModel } from '@fleio-api/billing/model/tax-rule.model';
import { TaxRulesApiService } from '@fleio-api/billing/tax-rules/tax-rules-api.service';
import { TaxRuleEditFormComponent } from './tabs/tax-rule-edit-form/tax-rule-edit-form.component';
import { RouterLinkAction } from '@objects-view/actions/router-link-action';
import { CallbackAction } from '@objects-view/actions/callback-action';
import { ApiCallAction, CallType } from '@objects-view/actions/api-call-action';
import { TaxRuleDetailsOverviewComponent } from './tabs/tax-rule-details-overview/tax-rule-details-overview.component';
import { EntityAuditLogComponent } from '../../common-components/entity-audit-log/entity-audit-log.component';

export class TaxRuleUIService extends ObjectUIServiceBase<ITaxRuleModel> {
  private readonly router: Router;
  private readonly config: ConfigService;
  private readonly taxRulesApiService: TaxRulesApiService;

  constructor(
    object: ITaxRuleModel, permissions: IPermissionsModel, state: string,
    router: Router, config: ConfigService, taxRulesApiService: TaxRulesApiService,
  ) {
    super(object, permissions, state);
    this.router = router;
    this.config = config;
    this.taxRulesApiService = taxRulesApiService;
  }

  getIcon(): IIcon {
    return null;
  }

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

  getTitle(): ITitle {
    switch (this.state) {
      case 'edit':
        return {
          text: $localize`Edit ${this.object.name}`
        };
      case 'create':
        return {
          text: $localize`Create tax rule`
        };
      default:
        return {
          text: `${this.object.name}`,
        };
    }
  }

  getActions(): IAction[] {
    const actions: IAction[] = [];
    actions.push(new RouterLinkAction({
      icon: {name: 'edit', class: 'fl-icons'},
      name: $localize`Edit`,
      noPermissions: !this.permissions['taxrules.update'],
      tooltip: $localize`Edit`,
      routerUrl: this.config.getPanelUrl(`billing/tax-rules/${this.object.id}/edit`),
      router: this.router,
      }
    ));

    actions.push(new ApiCallAction(
      {
        object: this.object,
        icon: {name: 'delete'},
        tooltip: $localize`Delete`,
        name: $localize`Delete`,
        noPermissions: !this.permissions['taxrules.destroy'],
        confirmOptions: {
          confirm: true,
          title: $localize`Delete tax rule`,
          message: $localize`Are you sure you want to delete tax rule ${this.object.name}?`,
        },
        successMessage: $localize`Tax rule deleted`,
        errorMessage: $localize`Failed to delete tax rule, check logs for details`,
        apiService: this.taxRulesApiService,
        callType: CallType.Delete,
        refreshAfterExecute: true,
        redirectAfterExecute: false,
        redirectUrl: this.config.getPanelUrl('billing/tax-rules'),
      }
    ));
    return actions;
  }

  getDetailsLink(): string {
    return this.config.getPanelUrl(`billing/tax-rules/${this.object.id}`);
  }

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

  getTabs(): IDetailsTab[] {
    switch (this.state) {
      case 'create':
        return [
          {
            tabName: $localize`Create`,
            tabIdentifier: 'create',
            component: TaxRuleEditFormComponent
          }
        ];
      case 'details':
        return [
          {
            tabName: $localize`Overview`,
            tabIdentifier: 'overview',
            component: TaxRuleDetailsOverviewComponent,
          },
          {
            tabName: $localize`Audit log`,
            tabIdentifier: 'audit-log',
            component: EntityAuditLogComponent,
            featureName: 'utils.activitylog',
            data: {
              entity_type: 'tax_rule'
            }
          }
        ];
      case 'edit':
        return [
          {
            tabName: $localize`Edit`,
            tabIdentifier: 'edit',
            component: TaxRuleEditFormComponent
          },
        ];
      default:
        return [];
    }
  }

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

  getDetailsActions(): IAction[] {
    const actions: IAction[] = [];
    switch (this.state) {
      case 'create':
        actions.push(new RouterLinkAction({
            name: $localize`Cancel`,
            routerUrl: this.config.getPrevUrl(`billing/tax-rules`),
            router: this.router,
          }
        ));
        actions.push(new CallbackAction({name: $localize`Create tax rule`}));
        break;
      case 'edit':
        actions.push(new RouterLinkAction({
            name: $localize`Cancel`,
            routerUrl: this.config.getPrevUrl(`billing/tax-rules`),
            router: this.router,
          }
        ));
        actions.push(new CallbackAction({name: $localize`Save`}));
        break;
      default:
        break;
    }
    return actions;
  }
}
