import { ObjectUIServiceBase } from '@objects-view/object-ui-service-base';
import { Router } from '@angular/router';
import { ConfigService } from '@shared/config/config.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 { IDataField } from '@objects-view/interfaces/card-data/data-field';
import { IDetailsTab } from '@objects-view/interfaces/details/details-tab';
import { InvitationOverviewComponent } from './tabs/invitation-overview/invitation-overview.component';
import { RouterLinkAction } from '@objects-view/actions/router-link-action';
import { CallbackAction } from '@objects-view/actions/callback-action';
import { DateFormatterPipe } from '@shared/ui/common/pipes/date-formatter.pipe';

export class InvitationUiService extends ObjectUIServiceBase<any> {
  private readonly datePipe: DateFormatterPipe;

  constructor(
    object: any, permissions: IPermissionsModel, state: string,
    private router: Router, private config: ConfigService,
  ) {
    super(object, permissions, state);
    this.datePipe = new DateFormatterPipe(this.config);
  }

  getIcon(): IIcon {
    return null;
  }

  getStatus(): IObjectStatus {
    return null;
  }

  getTitle(): ITitle {
    switch (this.state) {
      case 'details':
        let title;
        if (this.object?.id) {
          title = $localize`Invitation for ${this.object.client_full_name}`;
        } else {
          if (this.config.authService.userData?.user) {
            title = $localize`Already logged in`;
          } else {
            title = $localize`Invalid invitation or token`;
          }
        }
        return {
          text: title,
        };

      default:
        return {
          text: $localize`Invitation for ${this.object?.client_full_name}`,
        };
    }
  }

  getActions(): IAction[] {
    return [];
  }

  getDetailsActions(): IAction[] {
    const isLoggedIn = !!this.config.authService.userData?.user;
    if (isLoggedIn && this.object.token_invalid) {
      return [
        new RouterLinkAction({
            name: $localize`Go to dashboard`,
            routerUrl: this.config.getPanelHomeUrl(),
            router: this.router,
          }
        ),
        new CallbackAction({
          name: $localize`Log out and follow invitation link`,
        }),
      ];
    }
    return [];
  }

  getDetailsLink(): string {
    return this.config.getPanelUrl(`clients-users/invitations/${this.object.id}`);
  }

  getCardFields(): IDataField[] {
    return [];
  }

  getTabs(): IDetailsTab[] {
    switch (this.state) {
      case 'details':
        return [
          {
            tabName: $localize`Overview`,
            tabIdentifier: 'overview',
            component: InvitationOverviewComponent,
          },
        ];
    }
  }

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