import { useMemo } from 'react';
import { Admin, Resource } from 'react-admin';
import polyglotI18nProvider from 'ra-i18n-polyglot';
import { authProvider } from './auth/authProvider';
import { dataProvider } from './dataProvider';
import { useEmbeddedParentTheme } from './embed/useEmbeddedParentTheme';
import { messagesRu } from './i18n/ru';
import { EmbeddedActiveEquipmentPage } from './pages/EmbeddedActiveEquipmentPage';
import { buildToirMuiTheme } from './theme/toirMuiTheme';
import { EquipmentCreate } from './resources/equipment/EquipmentCreate';
import { EquipmentEdit } from './resources/equipment/EquipmentEdit';
import { EquipmentList } from './resources/equipment/EquipmentList';
import { EquipmentShow } from './resources/equipment/EquipmentShow';
import { EquipmentStatusChangeCreate } from './resources/equipment-status-change/EquipmentStatusChangeCreate';
import { EquipmentStatusChangeEdit } from './resources/equipment-status-change/EquipmentStatusChangeEdit';
import { EquipmentStatusChangeList } from './resources/equipment-status-change/EquipmentStatusChangeList';
import { EquipmentStatusChangeShow } from './resources/equipment-status-change/EquipmentStatusChangeShow';
import { ToirLayout } from './layout/ToirLayout';

function ToirAdmin() {
  const paletteMode = useEmbeddedParentTheme();
  const theme = useMemo(
    () => buildToirMuiTheme(paletteMode),
    [paletteMode],
  );
  const i18nProvider = useMemo(
    () =>
      polyglotI18nProvider(() => messagesRu, 'ru', {
        allowMissing: true,
      }),
    [],
  );

  return (
    <Admin
      layout={ToirLayout}
      dataProvider={dataProvider}
      authProvider={authProvider}
      theme={theme}
      i18nProvider={i18nProvider}
    >
      <Resource
        name="equipment"
        options={{ label: 'Оборудование' }}
        list={EquipmentList}
        create={EquipmentCreate}
        edit={EquipmentEdit}
        show={EquipmentShow}
      />
      <Resource
        name="status-changes"
        options={{ label: 'Журнал актов' }}
        list={EquipmentStatusChangeList}
        create={EquipmentStatusChangeCreate}
        edit={EquipmentStatusChangeEdit}
        show={EquipmentStatusChangeShow}
      />
    </Admin>
  );
}

function App() {
  if (window.location.pathname === '/embedded/equipment-active') {
    return <EmbeddedActiveEquipmentPage />;
  }

  return <ToirAdmin />;
}

export default App;
