import { RootStore } from 'stores/RootStore';
import { action, makeAutoObservable, toJS } from 'mobx';
import { ISettingsAuth, ISettingsTls } from 'types/default';
import moment from 'moment';

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

  get loading(): boolean {
    return this.rootStore.settingsStore.loading;
  }

  get settingsAuthentication(): ISettingsAuth | null {
    return toJS(this.rootStore.settingsStore.settingsAuth);
  }

  get settingsTlsObject(): ISettingsTls | null {
    return toJS(this.rootStore.settingsStore.settingsTls);
  }

  @action getSettingsAuthentication = (): void => {
    this.rootStore.settingsStore.getSettingsAuth();
  };

  @action getSettingsTls = (): void => {
    this.rootStore.settingsStore.getSettingsTls();
  };

  @action postSettingsTls = async (): Promise<void> => {
    await this.rootStore.settingsStore.postSettingsTls();
  };

  @action patchSettingsTls = async (data: ISettingsTls): Promise<void> => {
    await this.rootStore.settingsStore.patchSettingsTls(data);
  };

  @action deleteCerts = async (): Promise<void> => {
    await this.rootStore.settingsStore.deleteCertificates();
  };

  @action patchSettingsAuthentication = async (
    data: ISettingsAuth,
  ): Promise<void> => {
    const newData = {
      ...data,
      login_block_timeout: `${moment(data.login_block_timeout).format(
        'HH:mm:ss',
      )}`,
    };
    await this.rootStore.settingsStore.patchSettingsAuth(newData);
  };
}
