import { ObjectUIServiceBase } from '@objects-view/object-ui-service-base';
import { IServerModel } from '@fleio-api/servers/model/server.model';
import { Router } from '@angular/router';
import { ConfigService } from '@shared/config/config.service';
import { ServersApiService } from '@fleio-api/servers/servers-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, 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 { IServerGroupModel } from '@fleio-api/servers/model/server-group.model';
import { IDetailsTab } from '@objects-view/interfaces/details/details-tab';
import { ServerEditFormComponent } from './tabs/server-edit-form/server-edit-form.component';

export class ServerUiService extends ObjectUIServiceBase<IServerModel> {
  private readonly router: Router;
  private readonly config: ConfigService;
  private readonly serversApiService: ServersApiService;
  private readonly datePipe: DateFormatterPipe;

  static getPlacementDisplay(group: IServerGroupModel): string {
    if (group.placement === 1) {
      return $localize`In order`;
    }
    if (group.placement === 2) {
      return $localize`Least full`;
    }

    return $localize`n/a`;
  }

  constructor(
    server: IServerModel, permissions: IPermissionsModel, state: string,
    router: Router, config: ConfigService, serversApiService: ServersApiService
  ) {
    super(server, permissions, state);
    this.router = router;
    this.config = config;
    this.serversApiService = serversApiService;
    this.datePipe = new DateFormatterPipe(this.config);
  }

  getIcon(): IIcon {
    return null;
  }

  getStatus(): IObjectStatus {
    if (this.object.status) {
      switch (this.object.status) {
        case 'enabled':
          return {type: StatusType.Defined, value: StatusValue.Enabled};
        case 'disabled':
          return {type: StatusType.Defined, value: StatusValue.Disabled};
        default:
          return null;
      }
    }

    return null;
  }

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

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

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

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

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

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

    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(`servers/servers`),
            router: this.router,
          }
        ));
        actions.push(new CallbackAction({name: $localize`Create`}));
        break;
      case 'edit':
        actions.push(new RouterLinkAction({
            name: $localize`Cancel`,
            routerUrl: this.config.getPrevUrl(`servers/servers`),
            router: this.router,
          }
        ));
        actions.push(new CallbackAction({name: $localize`Save`}));
        break;
      default:
        break;
    }

    return actions;
  }

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

  getCardFields(): IDataField[] {
    const fields = [
      {name: $localize`Placement`, value: ServerUiService.getPlacementDisplay(this.object.group as IServerGroupModel)},
    ];

    return fields;
  }

  getTabs(): IDetailsTab[] {
    switch (this.state) {
      case 'create':
        return [
          {
            tabName: $localize`Create`,
            tabIdentifier: 'create',
            component: ServerEditFormComponent,
          },
        ];
      case 'edit':
        return [
          {
            tabName: $localize`Edit`,
            tabIdentifier: 'edit',
            component: ServerEditFormComponent,
          },
        ];
    }
  }

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