import { ObjectUIServiceBase } from '@objects-view/object-ui-service-base';
import { Router } from '@angular/router';
import { ConfigService } from '@shared/config/config.service';
import { DateFormatterPipe } from '@shared/ui/common/pipes/date-formatter.pipe';
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 { RouterLinkAction } from '@objects-view/actions/router-link-action';
import { ApiCallAction, CallType } from '@objects-view/actions/api-call-action';
import { CallbackAction } from '@objects-view/actions/callback-action';
import { IDataField } from '@objects-view/interfaces/card-data/data-field';
import { IDetailsTab } from '@objects-view/interfaces/details/details-tab';
import { IDomainModel } from '@fleio-api/plugins/domains/model/domain.model';
import { DomainApiService } from '@fleio-api/plugins/domains/domain-api.service';
import { DomainEditFormComponent } from './tabs/domain-edit-form/domain-edit-form.component';
import { DomainTransferFormComponent } from './tabs/domain-transfer-form/domain-transfer-form.component';
import { DomainRegisterFormComponent } from './tabs/domain-register-form/domain-register-form.component';


export class DomainUiService extends ObjectUIServiceBase<IDomainModel> {
  private readonly router: Router;
  private readonly config: ConfigService;
  private readonly domainApiService: DomainApiService;
  private readonly datePipe: DateFormatterPipe;


  constructor(
    domain: IDomainModel, permissions: IPermissionsModel, state: string,
    router: Router, config: ConfigService, domainApiService: DomainApiService,
  ) {
    super(domain, permissions, state);
    this.router = router;
    this.config = config;
    this.domainApiService = domainApiService;
    this.datePipe = new DateFormatterPipe(this.config);
  }

  getIcon(): IIcon {
    return null;
  }

  getStatus(): IObjectStatus {
    switch (this.object.status) {

      case 'undefined':
      case 'unmanaged':
        return {type: StatusType.Defined, value: StatusValue.Disabled};

      case 'pending':
      case 'pending_transfer':
        return {type: StatusType.Defined, value: StatusValue.Pending};
      case 'active':
      case 'grace':
      case 'redemption':
        return {type: StatusType.Defined, value: StatusValue.Active};
      case 'deleted':
        return {type: StatusType.Defined, value: StatusValue.Deleted};
      case 'expired':
      case 'canceled':
      case 'fraud':
        return {type: StatusType.Defined, value: StatusValue.Canceled};

      case 'transferred_away':
        return {type: StatusType.Defined, value: StatusValue.Deactivated};
    }
    return null;
  }

  getTitle(): ITitle {
    switch (this.state) {
      case 'details':
        return {
          text: `${this.object.name}`,
          subText: `${this.object.status_display}`,
        };

      case 'edit':
        return {
          text: $localize`Edit domain ${this.object.name}`,
        };

      case 'transfer':
        return {
          text: $localize`Transfer domain`,
        };

      case 'register':
        return {
          text: $localize`Register domain`,
        };

      default:
        return {
          text: `${this.object.name}`,
          subText: `${this.object.status_display}`,
        };
    }
  }

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

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

    const deleteAction = new ApiCallAction(
      {
        object: this.object,
        icon: {name: 'delete'},
        tooltip: $localize`Delete domain`,
        name: $localize`Delete`,
        confirmOptions: {
          confirm: true,
          title: $localize`Delete domain`,
          message: $localize`Are you sure you want to delete domain ${this.object.name}?`,
        },
        successMessage: $localize`Domain deleted`,
        errorMessage: $localize`Failed to delete domain, check logs for details`,
        apiService: this.domainApiService,
        callType: CallType.Delete,
        refreshAfterExecute: false,
        redirectAfterExecute: true,
        redirectUrl: this.config.getPanelUrl('domains'),
      }
    );

    actions.push(deleteAction);

    return actions;
  }

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

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

    return actions;
  }

  getDetailsLink(): string {
    return this.config.getPanelUrl(`domains/${this.object.service}`);
  }

  getCardFields(): IDataField[] {
    return [
      {name: $localize`Created at`, value: this.datePipe.toDateTime(this.object.created_at)},
      {
        name: $localize`Registrar`,
        value: this.object.last_registrar_name ? this.object.last_registrar_name : $localize`n/a`
      },
    ];
  }

  getTabs(): IDetailsTab[] {
    switch (this.state) {
      case 'edit':
        return [
          {
            tabName: $localize`Edit`,
            tabIdentifier: 'edit',
            component: DomainEditFormComponent,
          },
        ];
      case 'transfer':
        return [
          {
            tabName: $localize`Transfer`,
            tabIdentifier: 'transfer',
            component: DomainTransferFormComponent,
          },
        ];
      case 'register':
        return [
          {
            tabName: $localize`Register`,
            tabIdentifier: 'register',
            component: DomainRegisterFormComponent,
          },
        ];
    }
  }

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