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 { IGatewayModel } from '@fleio-api/billing/model/gateway.model';
import { GatewayEditFormComponent } from './tabs/gateway-edit-form/gateway-edit-form.component';
import { RouterLinkAction } from '@objects-view/actions/router-link-action';
import { CallbackAction } from '@objects-view/actions/callback-action';
import { GatewayDetailsOverviewComponent } from './tabs/gateway-details-overview/gateway-details-overview.component';
import { EntityAuditLogComponent } from '../../common-components/entity-audit-log/entity-audit-log.component';

export class GatewayUiService extends ObjectUIServiceBase<IGatewayModel> {
  private readonly router: Router;
  private readonly config: ConfigService;

  constructor(
    gateway: IGatewayModel, permissions: IPermissionsModel, state: string,
    router: Router, config: ConfigService,
  ) {
    super(gateway, permissions, state);
    this.router = router;
    this.config = config;
  }

  getIcon(): IIcon {
    return null;
  }

  getStatus(): IObjectStatus {
    if (this.object.enabled) {
      return {
        type: StatusType.Defined,
        value: StatusValue.Enabled
      };
    } else {
      return {
        type: StatusType.Defined,
        value: StatusValue.Disabled
      };
    }
  }

  getTitle(): ITitle {
    switch (this.state) {
      case 'edit':
        return {
          text: $localize`Edit gateway ${this.object.display_name}`,
        };

      default:
        return {
          text: `${this.object.display_name}`,
        };
    }
  }

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

    actions.push(new RouterLinkAction({
        icon: {name: 'edit', class: 'fl-icons'},
        tooltip: $localize`Edit gateway`,
        name: $localize`Edit`,
        routerUrl: this.config.getPanelUrl(`billing/gateways/${this.object.id}/edit`),
        router: this.router,
      }
    ));

    return actions;
  }

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

    switch (this.state) {
      case 'edit':
        actions.push(new RouterLinkAction({
            name: $localize`Cancel`,
            routerUrl: this.config.getPrevUrl(`billing/gateways`),
            router: this.router,
          }
        ));
        actions.push(new CallbackAction({name: $localize`Save`}));
        break;
      default:
        break;
    }

    return actions;
  }

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

  getCardFields(): IDataField[] {
    const fields = [
      {name: $localize`Enabled`, value: this.object.enabled ? $localize`yes` : $localize`no`},
      {name: $localize`Show to user`, value: this.object.visible_to_user ? $localize`yes` : $localize`no`},
      {name: $localize`Fixed fee`, value: this.object.fixed_fee},
      {name: $localize`Percent fee`, value: $localize`${this.object.percent_fee}%`},
    ];

    return fields;
  }

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

  getCardTags(): string[] {
    const tags = [];
    return tags;
  }
}
