import { IPermissionsModel } from '@fleio-api/base-model/IPermissionsModel';
import { IIcon } from '@shared/ui/common/interfaces/icon';
import { ITitle } from '@objects-view/interfaces/card-data/card-title';
import { IObjectStatus, StatusType, StatusValue } from '@objects-view/interfaces/object-status';
import { ObjectUIServiceBase } from '@objects-view/object-ui-service-base';
import { IAction } from '@objects-view/interfaces/actions/action';
import { Router } from '@angular/router';
import { ConfigService } from '@shared/config/config.service';
import { IDataField } from '@objects-view/interfaces/card-data/data-field';
import { IDetailsTab } from '@objects-view/interfaces/details/details-tab';
import { IClientModel } from '@fleio-api/client-user/model/client.model';
import { ClientsApiService } from '@fleio-api/client-user/client/clients-api.service';
import { DateFormatterPipe } from '@shared/ui/common/pipes/date-formatter.pipe';
import { RouterLinkAction } from '@objects-view/actions/router-link-action';
import { CallbackAction } from '@objects-view/actions/callback-action';
import {
  ClientSendMassMessageFormComponent
} from './tabs/client-send-mass-message-form/client-send-mass-message-form.component';

export class MassMessageUiService extends ObjectUIServiceBase<IClientModel> {
  private readonly router: Router;
  private readonly config: ConfigService;
  private readonly clientsApi: ClientsApiService;

  constructor(
    client: IClientModel, permissions: IPermissionsModel, state: string,
    router: Router, config: ConfigService, clientsApi: ClientsApiService
  ) {
    super(client, permissions, state);
    this.router = router;
    this.config = config;
    this.clientsApi = clientsApi;
  }

  getIcon(): IIcon {
    if (this.object && this.object.id) {
      return {
        name: '(avatar)',
        clientAvatar: this.object,
      };
    }
    return null;
  }

  getStatus(): IObjectStatus {
    switch (this.object.status) {
      case 'active':
        return {type: StatusType.Defined, value: StatusValue.Active};
      case 'inactive':
        return {type: StatusType.Defined, value: StatusValue.Disabled};
      case 'suspended':
        return {type: StatusType.Defined, value: StatusValue.Suspended};
      case 'deleting':
        return {type: StatusType.Changing, value: StatusValue.Deleted};
      default:
        return {type: StatusType.None, value: StatusValue.None};
    }
  }

  getTitle(): ITitle {
    let companySubText = '';
    if (this.object.company) {
      companySubText = this.object.company;
    }
    switch (this.state) {
      case 'send-mass-message':
        return {
          text: $localize`Send mass email and/or panel notification message to selected clients`
        };

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

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

    return actions;
  }

  getDetailsLink(): string {
    return null;
  }

  getCardFields(): IDataField[] {
    const datePipe = new DateFormatterPipe(this.config);
    const fields = [
      {
        name: $localize`ID`,
        value: `#${this.object.id.toString()}`,
      },
      {
        name: $localize`Created at`,
        value: `${datePipe.transform(this.object.date_created)}`,
        title: `${datePipe.getTitleAttributeValue(this.object.date_created)}`
      }
    ];

    if (this.object.outofcredit_datetime) {
      fields.push(
        {
          name: $localize`Out of credit since`,
          value: `${datePipe.toDateTime(this.object.outofcredit_datetime)}`
        }
      );
    } else {
      fields.push(
        {
          name: $localize`Up-to-date credit`,
          value: `${this.object.uptodate_credit}`
        }
      );
    }

    return fields;
  }

  getTabs(): IDetailsTab[] {
    switch (this.state) {
      case 'send-mass-message':
        return [
          {
            tabName: $localize`Send mass message`,
            tabIdentifier: 'send-mass-message',
            component: ClientSendMassMessageFormComponent,
          }
        ];
    }
  }

  getCardTags(): string[] {
    const tags: string[] = [];
    if (this.object.configuration_name) {
      tags.push(this.object.configuration_name);
    }
    if (this.object.status === 'suspended') {
      tags.push($localize`Suspended`);
    }

    return tags;
  }

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

    switch (this.state) {
      case 'send-mass-message':
        actions.push(new RouterLinkAction({
            name: $localize`Cancel`,
            routerUrl: this.config.getPrevUrl(`clients-users/clients`),
            router: this.router,
          }
        ));
        actions.push(new CallbackAction({name: $localize`Send messages`}));
        break;
      default:
        break;
    }

    return actions;
  }

  getObjectDetailsRefreshInterval(): number {
    return null;
  }
}
