import { makeAutoObservable, toJS } from 'mobx';
import { RootStore } from 'stores/RootStore';
import { TypeMessage } from './types';
import {
  Company,
  LocationCodeList,
  NCIRCCNotifictionList,
  NCIRCCNotifictionRetrive,
} from 'myApi';
import { IbulletainsMOCKDATA, ImesagesMOCKDATA } from './MOCK';
import { TypeCardSize } from 'types/default';
import { TypeDashboardPaginationState } from 'components/Dashboard/types';

export class NCIRCCUiStore {
  constructor(private readonly rootStore: RootStore) {
    makeAutoObservable(this);
  }

  selectedMessage: number | undefined = undefined;

  selectedBulletin: number | undefined = undefined;

  mockMessages = ImesagesMOCKDATA;

  mockBulletins = IbulletainsMOCKDATA;

  // Текущая страница таблицы
  pageCurrent = 1;

  // Число элементов на странице таблицы
  pageSize = 10;

  // Размер карточки сообщения
  messageCardSize: TypeCardSize = undefined;

  // Состояние загрузки сообщений
  isMessagesLoading = true;

  // Состояние пагинации таблицы
  get getMessagePaginationState(): TypeDashboardPaginationState {
    return {
      current: this.pageCurrent,
      pageSize: this.pageSize,
      total: this.countNotifications,
    };
  }

  // Смена пагинации таблицы сообщений
  setMessagePaginationState = (current: number, pageSize: number): void => {
    this.pageCurrent = current;
    this.pageSize = pageSize;
    this.messageCardSize = undefined;
  };

  // Задать размер карточки
  setMessagesCardSize = (newCardSize: TypeCardSize): void => {
    this.messageCardSize = newCardSize;
  };

  get getIsMessagesLoading(): boolean {
    return this.isMessagesLoading;
  }

  onShowMessage = async (id: number): Promise<void> => {
    this.messageCardSize = 'small';
    this.isMessagesLoading = true;
    await this.rootStore.ncirccStore.fetchCommentsOnId(id);
    this.setSelectedMessage(id);
    this.isMessagesLoading = false;
  };

  setSelectedMessage(id: number): void {
    this.selectedMessage = id;
  }

  setSelectedBulletin(id: number): void {
    this.selectedBulletin = id;
  }

  get getSelectedMessage(): number | undefined {
    return this.selectedMessage;
  }

  get getSelectedBulletin(): number | undefined {
    return this.selectedBulletin;
  }

  get getSelectedBulletinObject(): any {
    if (this.selectedBulletin)
      return toJS(
        this.mockBulletins.find((item) => item.id === this.selectedBulletin),
      );
  }

  get countNotifications(): number {
    return this.rootStore.ncirccStore.total;
  }

  get getCommentsOnId(): TypeMessage[] {
    return toJS(this.rootStore.ncirccStore.comments);
  }

  fetchCommentsOnId = async (id: number): Promise<void> => {
    await this.rootStore.ncirccStore.fetchCommentsOnId(id);
  };

  postCommentsOnId = async (text: string): Promise<void> => {
    if (this.selectedMessage) {
      await this.rootStore.ncirccStore.postCommentsOnId(
        this.selectedMessage,
        text,
      );
    }
  };

  get allNotification(): NCIRCCNotifictionList {
    return toJS(this.rootStore.ncirccStore.notifications);
  }

  fetchAllNotification = async (): Promise<void> => {
    await this.rootStore.ncirccStore.fetchAllNotification(
      this.getMessagePaginationState.current,
      this.getMessagePaginationState.pageSize,
    );
  };

  fetchLocation = async (): Promise<LocationCodeList> => {
    return this.rootStore.ncirccStore.fetchLocation();
  };

  fetchCompanyInfo = async (): Promise<Company> => {
    return this.rootStore.ncirccStore.fetchCompanyInfo();
  };

  postNotification = async (
    data: NCIRCCNotifictionRetrive,
  ): Promise<NCIRCCNotifictionRetrive> => {
    return this.rootStore.ncirccStore.postNotification(data);
  };
}
