import { ObjectUIServiceBase } from '@objects-view/object-ui-service-base';
import { ITLDModel } from '@fleio-api/plugins/domains/model/tld.model';
import { Router } from '@angular/router';
import { ConfigService } from '@shared/config/config.service';
import { TLDsApiService } from '@fleio-api/plugins/domains/tlds-api.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 } 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 { TldEditFormComponent } from './tabs/tld-edit-form/tld-edit-form.component';
import { TldPricingComponent } from './tabs/tld-pricing/tld-pricing.component';
import { TldAddonsPricingComponent } from './tabs/tld-addons-pricing/tld-addons-pricing.component';
import { TldRegistrarsComponent } from './tabs/tld-registrars/tld-registrars.component';
import { TldCostPricingComponent } from './tabs/tld-cost-pricing/tld-cost-pricing.component';
import { TldCustomFieldsComponent } from './tabs/tld-custom-fields/tld-custom-fields.component';
import { EntityAuditLogComponent } from '../../../common-components/entity-audit-log/entity-audit-log.component';

export class TLDUiService extends ObjectUIServiceBase<ITLDModel> {
  private readonly router: Router;
  private readonly config: ConfigService;
  private readonly tldsApiService: TLDsApiService;
  private readonly datePipe: DateFormatterPipe;


  constructor(
    tld: ITLDModel, permissions: IPermissionsModel, state: string,
    router: Router, config: ConfigService, tldsApiService: TLDsApiService,
  ) {
    super(tld, permissions, state);
    this.router = router;
    this.config = config;
    this.tldsApiService = tldsApiService;
    this.datePipe = new DateFormatterPipe(this.config);
  }

  getIcon(): IIcon {
    return null;
  }

  getStatus(): IObjectStatus {
    return null;
  }

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

      case 'create':
        return {
          text: $localize`Create new TLD`,
        };

      default:
        return {
          text: this.object.name,
        };
    }
  }

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

    const deleteAction = new ApiCallAction(
      {
        object: this.object,
        icon: {name: 'delete'},
        tooltip: $localize`Delete TLD`,
        name: $localize`Delete`,
        confirmOptions: {
          confirm: true,
          title: $localize`Delete TLD`,
          message: $localize`Are you sure you want to delete TLD ${this.object.name}?`,
        },
        successMessage: $localize`TLD deleted`,
        apiService: this.tldsApiService,
        callType: CallType.Delete,
        refreshAfterExecute: false,
        redirectAfterExecute: true,
        redirectUrl: this.config.getPanelUrl('domains/tlds'),
      }
    );

    actions.push(deleteAction);

    return actions;
  }

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

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

    return actions;
  }

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

  getCardFields(): IDataField[] {
    const fields = [
      {name: $localize`Created at`, value: this.datePipe.toDateTime(this.object.created_at)},
      {name: $localize`Premium domains available`, value: this.object.premium_domains_available ? $localize`Yes` : $localize`No`},
      {name: $localize`Whois server configured`, value: this.object.whois_server_configured ? $localize`Yes` : $localize`No`},
    ];

    return fields;
  }

  getTabs(): IDetailsTab[] {
    switch (this.state) {
      case 'details':
        return [
          {
            tabName: $localize`Pricing`,
            tabIdentifier: 'pricing',
            component: TldPricingComponent,
          },
          {
            tabName: $localize`Addons pricing`,
            tabIdentifier: 'addons-pricing',
            component: TldAddonsPricingComponent,
          },
          {
            tabName: $localize`Registrars`,
            tabIdentifier: 'registrars',
            component: TldRegistrarsComponent,
          },
          {
            tabName: $localize`Cost pricing`,
            tabIdentifier: 'cost-pricing',
            component: TldCostPricingComponent,
          },
          {
            tabName: $localize`Custom fields`,
            tabIdentifier: 'custom-fields',
            component: TldCustomFieldsComponent,
          },
          {
            tabName: $localize`Audit log`,
            tabIdentifier: 'audit-log',
            component: EntityAuditLogComponent,
            featureName: 'utils.activitylog',
            data: {
              entity_type: 'tld'
            }
          }
        ];
      case 'create':
        return [
          {
            tabName: $localize`Create`,
            tabIdentifier: 'create',
            component: TldEditFormComponent,
          },
        ];
    }
  }

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