import React from 'react';
import { useRouter } from 'next/router';

import { ACCESS_LEVEL_HUB, ACCESS_LEVEL_PRIVATE, SCHEDULE_TYPE_CUSTOM } from '@/modules/feeds/Editor/constants';

import {
  accessLevelSelector,
  isConnectedSelector,
  isSelfServeUserSelector,
  scheduleTypeSelector,
} from '@/modules/feeds/Editor/redux/selectors';

import { FormGroupTitle, FormRow } from '@/modules/@common/Form';
import { useAppSelector } from '@/modules/@common/store/hooks';
import AccessLevel from '@/modules/feeds/Editor/components/FormElements/AccessLevel/AccessLevel';
import AccessVideoOwner from '@/modules/feeds/Editor/components/FormElements/AccessVideoOwner/AccessVideoOwner';
import Account from '@/modules/feeds/Editor/components/FormElements/Account/Account';
import Authorization from '@/modules/feeds/Editor/components/FormElements/Authorization/Authorization';
import AutoImport from '@/modules/feeds/Editor/components/FormElements/AutoImport/AutoImport';
import DisplayName from '@/modules/feeds/Editor/components/FormElements/DisplayName/DisplayName';
import Hubs from '@/modules/feeds/Editor/components/FormElements/Hubs/Hubs';
import Language from '@/modules/feeds/Editor/components/FormElements/Language/Language';
import LoadFromLastDays from '@/modules/feeds/Editor/components/FormElements/LoadFromLastDays/LoadFromLastDays';
import Name from '@/modules/feeds/Editor/components/FormElements/Name/Name';
import ScheduleFrequency from '@/modules/feeds/Editor/components/FormElements/ScheduleFrequency/ScheduleFrequency';
import SchedulePeriod from '@/modules/feeds/Editor/components/FormElements/SchedulePeriod/SchedulePeriod';
import ScheduleStartTime from '@/modules/feeds/Editor/components/FormElements/ScheduleStartTime/ScheduleStartTime';

function GeneralTab() {
  const router = useRouter();
  const id = parseInt(router.query.id as string, 10);

  const accessLevel = useAppSelector(accessLevelSelector);
  const scheduleType = useAppSelector(scheduleTypeSelector);
  const isConnected = useAppSelector(isConnectedSelector);

  const isSelfServeUser = useAppSelector(isSelfServeUserSelector);

  return (
    <>
      <FormRow label="Authorization">
        <Authorization />
      </FormRow>
      <FormGroupTitle>Settings</FormGroupTitle>
      {!isSelfServeUser && (
        <FormRow label="Account">
          <Account disabled={!!id || !isConnected} />
        </FormRow>
      )}

      {!isSelfServeUser && (
        <FormRow label="Source Name">
          <Name disabled={!!id || !isConnected} />
        </FormRow>
      )}
      <FormRow label="Display Name">
        <DisplayName disabled={!isConnected} />
      </FormRow>
      <FormRow label="Language">
        <Language disabled={!isConnected} />
      </FormRow>
      <FormRow label="Access Level">
        <AccessLevel disabled={!isConnected} />
      </FormRow>
      {accessLevel === ACCESS_LEVEL_HUB && (
        <FormRow label="Select Hubs">
          <Hubs disabled={!isConnected} />
        </FormRow>
      )}
      {accessLevel === ACCESS_LEVEL_PRIVATE && (
        <FormRow label="Select Video Owners">
          <AccessVideoOwner disabled={!isConnected} />
        </FormRow>
      )}

      <FormGroupTitle>Schedule</FormGroupTitle>
      <FormRow label="Auto Import">
        <AutoImport disabled={!isConnected} />
      </FormRow>

      <FormRow label="Schedule Period">
        <SchedulePeriod disabled={!isConnected} />
      </FormRow>
      <FormRow label="Start time">
        <ScheduleStartTime disabled={!isConnected} />
      </FormRow>
      {scheduleType === SCHEDULE_TYPE_CUSTOM && (
        <FormRow label="Frequency (min)">
          <ScheduleFrequency disabled={!isConnected} />
        </FormRow>
      )}
      <FormRow label="Lookback Window">
        <LoadFromLastDays disabled={!isConnected} />
      </FormRow>
    </>
  );
}

export default GeneralTab;
