import { action, makeObservable, observable } from 'mobx';
import { loadable } from './Decorators/LoadingDecorator';
import { EntityStore } from './EntityStore';
import { Account } from './AccountsStore';
import { RootStore } from './RootStore';
import { IRotationSettings } from 'types/default';
import { openNotificationSuccess } from 'components/Messages';
import request from 'services/request';

export class RotationStore extends EntityStore<Account> {
  constructor(private readonly rootStore: RootStore) {
    super();
    makeObservable(this);
  }

  @observable rotationIncidents: IRotationSettings | null = null;

  @observable rotationEvents: IRotationSettings | null = null;

  @action getRotationIncidents(): void {
    request({
      method: 'get',
      url: `rotation/incidents/current_settings/`,
    }).then(
      action((response: any) => {
        this.rotationIncidents = response.data;
      }),
    );
  }

  @loadable
  getRotationEvents(): void {
    request({
      method: 'get',
      url: `rotation/events/current_settings/`,
    }).then(
      action((response: any) => {
        this.rotationEvents = response.data;
      }),
    );
  }

  @loadable
  postRotationIncidents(
    data: any,
    {
      messageTitle,
      messageDescription,
    }: { messageTitle: string; messageDescription: string },
  ): void {
    request({
      method: 'post',
      url: `rotation/incidents/set/`,
      data,
    }).then(() => {
      openNotificationSuccess({
        messageTitle,
        messageDescription,
      });
      this.getRotationIncidents();
    });
  }

  @loadable
  postRotationEvents(
    data: any,
    {
      messageTitle,
      messageDescription,
    }: { messageTitle: string; messageDescription: string },
  ): void {
    request({
      method: 'post',
      url: `rotation/events/set/`,
      data,
    }).then(() => {
      this.getRotationEvents();
      openNotificationSuccess({
        messageTitle,
        messageDescription,
      });
    });
  }
}
