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 { 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 { MatDialog } from '@angular/material/dialog';
import { IServiceCycleModel, ServiceCycleStates } from '@fleio-api/billing/service-cycles/models/service-cycle-model';
import { ServiceCyclesApiService } from '@fleio-api/billing/service-cycles/service-cycles-api.service';
import { CallbackAction } from '@objects-view/actions/callback-action';
import { StaticObjectController } from '@objects-view/static-object-controller';
import {
  ObjectDetailsTabDialogComponent
} from '@objects-view/object-details-tab-dialog/object-details-tab-dialog.component';
import { ServiceCycleEditFormComponent } from './tabs/service-cycle-edit-form/service-cycle-edit-form.component';
import { IActionResult } from '@objects-view/interfaces/actions/action-result';
import { Observable } from 'rxjs';
import { RefreshService } from '@shared/ui-api/refresh.service';
import { ApiCallAction, CallType } from '@objects-view/actions/api-call-action';
import { ServiceCycleEditTrackerService } from './service-cycle-edit-tracker.service';

export class ServiceCycleUIService extends ObjectUIServiceBase<IServiceCycleModel> {

  constructor(
    serviceCycle: IServiceCycleModel,
    permissions: IPermissionsModel,
    state: string,
    private readonly config: ConfigService,
    private readonly serviceCyclesApiService: ServiceCyclesApiService,
    private readonly matDialog: MatDialog,
    private readonly refreshService: RefreshService,
    private serviceCycleEditTrackerService: ServiceCycleEditTrackerService,
  ) {
    super(serviceCycle, permissions, state);
  }

  getIcon(): IIcon {
    return null;
  }

  getStatus(): IObjectStatus {
    switch (this.object.state) {
      case ServiceCycleStates.InProgress:
        return {type: StatusType.Defined, value: StatusValue.Pending};
      case ServiceCycleStates.Invoiced:
        return {type: StatusType.Defined, value: StatusValue.Pending};
      case ServiceCycleStates.Paid:
        return {type: StatusType.Defined, value: StatusValue.Active};
      case ServiceCycleStates.PaymentCanceled:
        return {type: StatusType.Defined, value: StatusValue.Canceled};
      case ServiceCycleStates.Refunded:
        return {type: StatusType.Defined, value: StatusValue.Canceled};
      case ServiceCycleStates.Unpaid:
        return {type: StatusType.Defined, value: StatusValue.Error};
      default:
        return {type: StatusType.None, value: StatusValue.None};
    }
  }

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

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

  getActions(): IAction[] {
    const actions: IAction[] = [];
    actions.push(new CallbackAction({
      icon: {name: 'edit'},
      name: $localize`Edit`,
      tooltip: $localize`Edit service cycle`,
      refreshAfterExecute: true,
      callback: () => new Observable<IActionResult>(subscriber => {
        this.matDialog.open(
          ObjectDetailsTabDialogComponent, {
            data: {
              dialogTitle: $localize`Edit service cycle`,
              objectController: new StaticObjectController(
                {
                  tabs: [
                    {
                      tabName: $localize`Edit`,
                      tabIdentifier: 'edit',
                      component: ServiceCycleEditFormComponent,
                    },
                  ],
                  detailsActions: [
                    new CallbackAction({
                      name: $localize`Save`,
                      primary: true,
                    }),
                  ],
                },
                this.object,
              ),
              additionalClasses: [],
            },
            autoFocus: false,
          }).afterClosed().subscribe(() => {
          subscriber.next(null);
          subscriber.complete();
          this.refreshService.refresh();
        });
      })
    }));

    actions.push(new ApiCallAction(
      {
        object: this.object,
        icon: {name: 'delete'},
        name: $localize`Delete`,
        tooltip: $localize`Delete service cycle`,
        confirmOptions: {
          confirm: true,
          title: $localize`Delete service cycle`,
          message: $localize`Are you sure you want to delete service cycle #${this.object.id}?`,
        },
        successMessage: $localize`Service cycle deleted`,
        apiService: this.serviceCyclesApiService,
        callType: CallType.Delete,
        refreshAfterExecute: true,
        redirectAfterExecute: false,
        postExecuteCallback: () => {
          // service cycles may have changed, ensure they refresh
          this.serviceCycleEditTrackerService.editedServiceCycle();
        }
      }
    ));
    return actions;
  }

  getDetailsLink(): string {
    return null;
  }

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

  getTabs(): IDetailsTab[] {
    return [];
  }

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

  getDetailsActions(): IAction[] {
    return [] as Array<IAction>;
  }
}
