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 { IObjectStatus } from '@objects-view/interfaces/object-status';
import { IIcon } from '@shared/ui/common/interfaces/icon';
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 { IContainerModel } from '@fleio-api/openstack/object-store/model/container.model';
import { ContainersApiService } from '@fleio-api/openstack/object-store/containers/containers-api.service';
import { RouterLinkAction } from '@objects-view/actions/router-link-action';
import { CallbackAction } from '@objects-view/actions/callback-action';
import { ContainerEditFormComponent } from './tabs/container-edit-form/container-edit-form.component';
import { ApiCallAction, CallType } from '@objects-view/actions/api-call-action';
import {
  ContainerDetailsOverviewComponent
} from './tabs/container-details-overview/container-details-overview.component';
import {
  ContainerObjectUploadFormComponent
} from './tabs/container-object-upload-form/container-object-upload-form.component';
import {
  ContainerFolderCreateFormComponent
} from './tabs/container-folder-create-form/container-folder-create-form.component';
import {
  ContainerObjectCopyFormComponent
} from './tabs/container-object-copy-form/container-object-copy-form.component';
import { FormatHelper } from '@shared/ui-api/helpers/format-helper';
import { RouteHelper } from '@shared/ui-api/route-helper';
import { IDetailsLink } from '@objects-view/interfaces/details-link';

export class ContainerUiService extends ObjectUIServiceBase<IContainerModel> {
  private readonly router: Router;
  private readonly config: ConfigService;
  private readonly containersApiService: ContainersApiService;
  private readonly datePipe: DateFormatterPipe;
  private readonly formatHelper = new FormatHelper();


  constructor(
    containerModel: IContainerModel, permissions: IPermissionsModel, state: string,
    router: Router, config: ConfigService, containersApiService: ContainersApiService
  ) {
    super(containerModel, permissions, state);
    this.router = router;
    this.config = config;
    this.containersApiService = containersApiService;
    this.datePipe = new DateFormatterPipe(this.config);
  }

  getIcon(): IIcon {
    return null;
  }

  getStatus(): IObjectStatus {
    return null;
  }

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

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

      case 'folder-create':
        return {
          text: $localize`Create new folder in ${this.object.name}:name:`,
        };

      case 'object-upload':
        return {
          text: $localize`Upload file in ${this.object.name}:name:`,
        };

      case 'object-copy':
        return {
          text: $localize`Copy file from ${this.object.name}:name:`,
        };

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

  getActions(): IAction[] {
    const actions: IAction[] = [
      new RouterLinkAction({
          icon: {name: 'edit', class: 'fl-icons'},
          tooltip: $localize`Edit container`,
          name: $localize`Edit`,
          routerUrl: this.config.getPanelUrl(`openstack/object-store/${this.object.id}/edit`),
          router: this.router,
          propagateQueryParams: ['region'],
        }
      ),

      new ApiCallAction(
        {
          object: this.object,
          icon: {name: 'delete'},
          name: $localize`Delete`,
          tooltip: this.object.object_count > 0 ? $localize`Cannot delete - container needs to be empty` : $localize`Delete`,
          confirmOptions: {
            confirm: true,
            title: $localize`Delete container`,
            message: $localize`Are you sure you want to delete container ${this.object.name}:name:?`,
          },
          disabled: this.object.object_count > 0,
          apiService: this.containersApiService,
          callType: CallType.Delete,
          refreshAfterExecute: false,
          redirectAfterExecute: true,
          redirectUrl: this.config.getPanelUrl('openstack/object-store'),
          propagateQueryParams: ['region'],
        }
      )
    ];

    return actions;
  }

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

    switch (this.state) {
      case 'create':
        actions.push(new RouterLinkAction({
            name: $localize`Cancel`,
            routerUrl: this.config.getPrevUrl(`openstack/object-store`),
            router: this.router,
            propagateQueryParams: ['region'],
          }
        ));
        actions.push(new CallbackAction({name: $localize`Create`}));
        break;
      case 'edit':
        actions.push(new RouterLinkAction({
            name: $localize`Cancel`,
            routerUrl: this.config.getPanelUrl(`openstack/object-store`, this.object.id),
            router: this.router,
            propagateQueryParams: ['region'],
          }
        ));
        actions.push(new CallbackAction({name: $localize`Save`}));
        break;
      case 'object-upload':
        actions.push(new RouterLinkAction({
            name: $localize`Cancel`,
            routerUrl: this.config.getPanelUrl(`openstack/object-store`, this.object.id),
            router: this.router,
            propagateQueryParams: ['region'],
          }
        ));
        actions.push(new CallbackAction({name: $localize`Upload file`}));
        break;
      case 'object-copy':
        actions.push(new RouterLinkAction({
            name: $localize`Cancel`,
            routerUrl: this.config.getPanelUrl(`openstack/object-store`, this.object.id),
            router: this.router,
            propagateQueryParams: ['region'],
          }
        ));
        actions.push(new CallbackAction({name: $localize`Copy file`}));
        break;
      case 'folder-create':
        actions.push(new RouterLinkAction({
            name: $localize`Cancel`,
            routerUrl: this.config.getPanelUrl(`openstack/object-store`, this.object.id),
            router: this.router,
            propagateQueryParams: ['region'],
          }
        ));
        actions.push(new CallbackAction({name: $localize`Create folder`}));
        break;
      default:
        break;
    }

    return actions;
  }

  getDetailsLink(): IDetailsLink {
    const queryParams = RouteHelper.getQueryParams(this.router, ['region']);
    return {
      enabled: true,
      url: this.config.getPanelUrl(`openstack/object-store`, this.object.id),
      queryParams,
    };
  }

  getCardFields(): IDataField[] {
    const fields = [
      {name: $localize`Object count`, value: this.object.object_count.toString()},
      {name: $localize`Size`, value: this.formatHelper.readableBytes(this.object.size)},
      {name: $localize`Link`, value: this.object.name, url: this.object.link},
    ] as IDataField[];

    return fields;
  }

  getTabs(): IDetailsTab[] {
    switch (this.state) {
      case 'create':
        return [
          {
            tabName: $localize`Create`,
            tabIdentifier: 'create',
            component: ContainerEditFormComponent,
          },
        ];
      case 'edit':
        return [
          {
            tabName: $localize`Edit`,
            tabIdentifier: 'edit',
            component: ContainerEditFormComponent,
          },
        ];
      case 'details':
        return [
          {
            tabName: $localize`Overview`,
            tabIdentifier: 'overview',
            component: ContainerDetailsOverviewComponent,
          },
        ];
      case 'object-upload':
        return [
          {
            tabName: $localize`Upload`,
            tabIdentifier: 'upload',
            component: ContainerObjectUploadFormComponent,
          },
        ];
      case 'object-copy':
        return [
          {
            tabName: $localize`Copy`,
            tabIdentifier: 'copy',
            component: ContainerObjectCopyFormComponent,
          },
        ];
      case 'folder-create':
        return [
          {
            tabName: $localize`Upload`,
            tabIdentifier: 'upload',
            component: ContainerFolderCreateFormComponent,
          },
        ];
    }
  }

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