import { ObjectUIServiceBase } from '@objects-view/object-ui-service-base';
import { IRouterModel } from '@fleio-api/openstack/model/router.model';
import { Router } from '@angular/router';
import { ConfigService } from '@shared/config/config.service';
import { RoutersApiService } from '@fleio-api/openstack/routers/routers-api.service';
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 { RouterEditFormComponent } from '@shared/common-tabs/openstack/routers/router-edit-form/router-edit-form.component';
import { RouterDetailsOverviewComponent } from '@shared/common-tabs/openstack/routers/router-details-overview/router-details-overview.component';
import { RouterAddInterfaceFormComponent } from '@shared/common-tabs/openstack/routers/router-add-interface-form/router-add-interface-form.component';
import {
  RouterStaticRoutesFormComponent
} from '@shared/common-tabs/openstack/routers/router-static-routes-form/router-static-routes-form.component';

export class RouterUiService extends ObjectUIServiceBase<IRouterModel> {
  private readonly router: Router;
  private readonly config: ConfigService;
  private readonly routersApiService: RoutersApiService;

  constructor(
    routerModel: IRouterModel, permissions: IPermissionsModel, state: string,
    router: Router, config: ConfigService, routersApiService: RoutersApiService
  ) {
    super(routerModel, permissions, state);
    this.router = router;
    this.config = config;
    this.routersApiService = routersApiService;
  }

  getIcon(): IIcon {
    return null;
  }

  getStatus(): IObjectStatus {
    return null;
  }

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

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

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

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

    actions.push(new RouterLinkAction({
        icon: {name: 'edit', class: 'fl-icons'},
        name: $localize`Edit`,
        tooltip: $localize`Edit router`,
        routerUrl: this.config.getPanelUrl(`openstack/routers/${this.object.id}/edit`),
        router: this.router,
        noPermissions: !this.permissions['routers.update'],
      }
    ));

    actions.push(new RouterLinkAction({
        icon: {name: 'playlist_add'},
        name: $localize`Add interface`,
        tooltip: $localize`Add interface`,
        routerUrl: this.config.getPanelUrl(`openstack/routers/${this.object.id}/add-interface`),
        router: this.router,
        noPermissions: !this.permissions['routers.add_interface'],
      }
    ));

    const deleteAction = new ApiCallAction(
      {
        object: this.object,
        icon: {name: 'delete'},
        tooltip: $localize`Delete router`,
        name: $localize`Delete`,
        confirmOptions: {
          confirm: true,
          title: $localize`Delete router`,
          message: $localize`Are you sure you want to delete router ${this.object.name}?`,
        },
        successMessage: $localize`Router delete queued`,
        errorMessage: $localize`Failed to delete router, check logs for details`,
        apiService: this.routersApiService,
        callType: CallType.Delete,
        refreshAfterExecute: false,
        redirectAfterExecute: true,
        redirectUrl: this.config.getPanelUrl('openstack/routers'),
        noPermissions: !this.permissions['routers.destroy'],
      }
    );

    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(`openstack/routers`),
            router: this.router,
          }
        ));
        actions.push(new CallbackAction({name: $localize`Create`}));
        break;
      case 'add-interface':
        actions.push(new RouterLinkAction({
            name: $localize`Cancel`,
            routerUrl: this.config.getPrevUrl(`openstack/routers`),
            router: this.router,
          }
        ));
        actions.push(new CallbackAction({name: $localize`Add interface`}));
        break;
      case 'edit':
        actions.push(new RouterLinkAction({
            name: $localize`Cancel`,
            routerUrl: this.config.getPrevUrl(`openstack/routers`),
            router: this.router,
          }
        ));
        actions.push(new CallbackAction({name: $localize`Save`}));
        break;
      case 'details':
        actions.push(new CallbackAction({name: $localize`Update static routes`}));
        break;
      default:
        break;
    }

    return actions;
  }

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

  getCardFields(): IDataField[] {
    const fields = [
      {name: $localize`Gateway`, value: `${this.object.network_name}`},
      {name: $localize`Region`, value: this.object.region},
      {name: $localize`Description`, value: this.object.description || $localize`n/a`},
    ];

    return fields;
  }

  getTabs(): IDetailsTab[] {
    switch (this.state) {
      case 'create':
        return [
          {
            tabName: $localize`Create`,
            tabIdentifier: 'create',
            component: RouterEditFormComponent,
          },
        ];
      case 'details':
        return [
          {
            tabName: $localize`Details`,
            tabIdentifier: 'details',
            component: RouterDetailsOverviewComponent,
            hideActions: true,
          },
          {
            tabName: $localize`Static routes`,
            tabIdentifier: 'static-routes',
            component: RouterStaticRoutesFormComponent,
          },
        ];
      case 'add-interface':
        return [
          {
            tabName: $localize`Add interface`,
            tabIdentifier: 'add-interface',
            component: RouterAddInterfaceFormComponent,
          },
        ];
      case 'edit':
        return [
          {
            tabName: $localize`Edit`,
            tabIdentifier: 'edit',
            component: RouterEditFormComponent,
          },
        ];
    }
  }

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

  getObjectDetailsRefreshInterval(): number {
    switch (this.state) {
      case 'details':
        return this.config?.current?.settings?.refreshIntervals?.routerDetailsInterval || this.config.getDefaultRefreshInterval(true);
      default:
        return null;
    }
  }
}
