import { defineMessage } from 'react-intl';

import { patchMe } from 'pl-fe/actions/me';
import { getClient } from 'pl-fe/api';
import { NODE_ENV } from 'pl-fe/build-config';
import messages from 'pl-fe/messages';
import { selectOwnAccount } from 'pl-fe/selectors';
import KVStore from 'pl-fe/storage/kv-store';
import { useSettingsStore } from 'pl-fe/stores/settings';
import toast from 'pl-fe/toast';
import { isLoggedIn } from 'pl-fe/utils/auth';

import type { AppDispatch, RootState } from 'pl-fe/store';

const FE_NAME = NODE_ENV === 'production' ? 'pl_fe' : 'pl_fe_dev';

/** Options when changing/saving settings. */
type SettingOpts = {
  /** Whether to display an alert when settings are saved. */
  showAlert?: boolean;
  save?: boolean;
}

const saveSuccessMessage = defineMessage({ id: 'settings.save.success', defaultMessage: 'Your preferences have been saved!' });

const changeSetting = (path: string[], value: any, opts?: SettingOpts) => {
  useSettingsStore.getState().actions.changeSetting(path, value);

  if (opts?.save !== false) return saveSettings(opts);
  return () => {};
};

const saveSettings = (opts?: SettingOpts) =>
  (dispatch: AppDispatch, getState: () => RootState) => {
    if (!isLoggedIn(getState)) return;

    const { userSettings, actions: { userSettingsSaving } } = useSettingsStore.getState();
    if (userSettings.saved) return;

    const { saved, ...data } = userSettings;

    dispatch(updateSettingsStore(data)).then(() => {
      userSettingsSaving();

      if (opts?.showAlert) {
        toast.success(saveSuccessMessage);
      }
    }).catch(error => {
      toast.showAlertForError(error);
    });
  };

/** Update settings store for Mastodon, etc. */
const updateAuthAccount = async (url: string, settings: any) => {
  const key = `authAccount:${url}`;
  const oldAccount: any = await KVStore.getItem(key);
  try {
    if (!oldAccount) return;
    if (!oldAccount.settings_store) oldAccount.settings_store = {};
    oldAccount.settings_store[FE_NAME] = settings;
    await KVStore.setItem(key, oldAccount);
  } catch (error) {
    console.error(error);
  }
};

const updateSettingsStore = (settings: any) =>
  (dispatch: AppDispatch, getState: () => RootState) => {
    const state = getState();
    const client = getClient(state);

    if (client.features.frontendConfigurations) {
      return dispatch(patchMe({
        settings_store: {
          [FE_NAME]: settings,
        },
      }));
    } else {
      const accountUrl = selectOwnAccount(state)!.url;

      return updateAuthAccount(accountUrl, settings);
    }
  };

const getLocale = (fallback = 'en') => {
  const localeWithVariant = useSettingsStore.getState().settings.locale.replace('_', '-');
  const locale = localeWithVariant.split('-')[0];
  return Object.keys(messages).includes(localeWithVariant) ? localeWithVariant : Object.keys(messages).includes(locale) ? locale : fallback;
};

export {
  FE_NAME,
  changeSetting,
  saveSettings,
  updateSettingsStore,
  getLocale,
};
