import React, { useEffect } from 'react';
import { useDispatch, useSelector, useStore } from 'react-redux';
import { useRouter } from 'next/router';

import { TAB_DELIVERY, TAB_GENERAL, TAB_TARGETING } from '../constants';

import * as selectors from '../redux/selectors';
import {
  createItemAction,
  getItemAction,
  getTimezoneOptionsAction,
  setActiveTabIdAction,
  setErrorByPropAction,
  setInitialAction,
  setScrollToFieldNameAction,
  updateItemAction,
  validateFields,
} from '../redux/slices';

import { Form, FormContent } from '@/modules/@common/Form';
import DeliveryTab from './Tabs/DeliveryTab/DeliveryTab';
import GeneralTab from './Tabs/GeneralTab/GeneralTab';
import TargetingTab from './Tabs/TargetingTab/TargetingTab';
import { Button, Stack, Tab, TabContent, Tabs, Typography } from '@/mui/components';

import styles from './Editor.module.scss';

function Editor() {
  const store = useStore();
  const dispatch = useDispatch();
  const router = useRouter();

  const activeTabId = useSelector(selectors.activeTabIdSelector);
  const name = useSelector(selectors.nameSelector);

  const id = parseInt(router.query.id, 10);
  const isDuplicate = router.asPath.split('/').filter(Boolean).reverse()[0] === 'duplicate';

  const tabs = [
    {
      title: 'General',
      id: TAB_GENERAL,
      content: GeneralTab,
    },
    {
      title: 'Delivery',
      id: TAB_DELIVERY,
      content: DeliveryTab,
    },
    {
      title: 'Targeting',
      id: TAB_TARGETING,
      content: TargetingTab,
    },
  ].filter(Boolean);

  const getTitle = () => {
    if (id && !isDuplicate) {
      return `${name} > Settings`;
    }

    if (isDuplicate) {
      return 'Duplicate X-Ray Line Item';
    }

    return 'New X-Ray Line Item';
  };

  const saveToServerForm = () => {
    const state = store.getState();
    const allProps = selectors.fullAccessToStoreFieldsForValidation(state);

    const { validation, errorList } = validateFields(
      selectors
        .schemeSelector(state)
        .filter(({ tabId }) => tabs.some((tab) => tab.id === tabId))
        .map(({ fieldName }) => fieldName),
      allProps,
    );

    if (errorList.length) {
      const errorField = errorList.find((error) => error.tabId === activeTabId) ?? errorList[0];

      dispatch(setActiveTabIdAction(errorField.tabId));
      dispatch(setScrollToFieldNameAction(errorField.fieldName));
    } else if (id && !isDuplicate) {
      dispatch(updateItemAction(id));
    } else {
      // if duplicate need pass parent id
      dispatch(createItemAction(id));
    }

    dispatch(setErrorByPropAction(validation));
  };

  useEffect(() => {
    if (id) {
      dispatch(getItemAction({ id, isDuplicate }));
    }

    dispatch(getTimezoneOptionsAction());

    return () => {
      dispatch(setInitialAction());
    };
  }, [id]);

  return (
    <div className={styles.Wrapper}>
      <Stack direction="column" fullWidth justifyContent="center" className={styles.Title}>
        <Typography component="div" variant="h6" noWrap>
          {getTitle()}
        </Typography>
      </Stack>
      <Stack
        spacing={1}
        fullWidth
        direction="row"
        alignItems="center"
        justifyContent="space-between"
        className={styles.Controls}
      >
        {tabs.length > 1 && (
          <Tabs
            size="medium"
            orientation="horizontal"
            value={activeTabId}
            onChange={(event, value) => dispatch(setActiveTabIdAction(value))}
            className={styles.Tabs}
          >
            {tabs.map((tab) => (
              <Tab key={tab.id} label={tab.title} value={tab.id} />
            ))}
          </Tabs>
        )}
        <Stack spacing={2} justifyContent="flex-end" alignItems="center" direction="row">
          <Button
            variant="outlined"
            size="small"
            color="primary"
            shape="rounded"
            onClick={() => router.push('/x-ray/line-items')}
          >
            Cancel
          </Button>
          <Button variant="contained" size="small" color="primary" shape="rounded" onClick={saveToServerForm}>
            Save
          </Button>
        </Stack>
      </Stack>
      <Form scrollFieldSelector={selectors.scrollFieldSelector}>
        <FormContent>
          {tabs.map((tab) => {
            const Content = tab.content;

            return (
              <TabContent key={tab.id} component={FormContent} selectedId={activeTabId} id={tab.id}>
                <Content />
              </TabContent>
            );
          })}
        </FormContent>
      </Form>
    </div>
  );
}

export default Editor;
