import { Box } from "@mui/material";
import { Grid } from "../components/grid/Grid";
import { SPACING } from "../data/spacing";
import { useLocation } from "react-router-dom";
import {
  ROUTE_ADMINX_ROUTE_GUILDS,
  ROUTE_ADMINX_ROUTE_IN_PROVINCE_GUILDS_DISTRIBUTIONS,
  ROUTE_ADMINX_ROUTE_IN_PROVINCE_GUILDS_REQUESTS,
  ROUTE_ADMINX_ROUTE_GUILDS_SETTINGS,
  ROUTE_ADMINX_ROUTE_IN_PROVINCE_GUILDS,
  ROUTE_ADMINX_ROUTE_MANAGE_GUILDS,
  ROUTE_ADMINX_ROUTE_OUT_PROVINCE_GUILDS,
  ROUTE_ADMINX_ROUTE_IN_PROVINCE_STEWARDS,
  ROUTE_CITY_JIHAD_ROUTE_GUILDS,
  ROUTE_CITY_JIHAD_ROUTE_MANAGE_GUILDS,
  ROUTE_CITY_JIHAD_ROUTE_STEWARDS,
  ROUTE_CITY_POULTRY_ROUTE_GUILDS,
  ROUTE_CITY_POULTRY_ROUTE_MANAGE_GUILDS,
  ROUTE_CITY_POULTRY_ROUTE_STEWARDS,
  ROUTE_COMMERCE_ROUTE_GUILDS,
  ROUTE_COMMERCE_ROUTE_GUILDS_SETTINGS,
  ROUTE_COMMERCE_ROUTE_MANAGE_GUILDS,
  ROUTE_COMMERCE_ROUTE_STEWARDS,
  ROUTE_GUILD_ROOM_ROUTE_GUILDS,
  ROUTE_GUILD_ROOM_ROUTE_GUILDS_REQUESTS,
  ROUTE_GUILD_ROOM_ROUTE_MANAGE_GUILDS,
  ROUTE_GUILD_ROOM_ROUTE_STEWARDS,
  ROUTE_PROVINCE_ROUTE_GUILDS,
  ROUTE_PROVINCE_ROUTE_IN_PROVINCE_GUILDS_REQUESTS,
  ROUTE_PROVINCE_ROUTE_GUILDS_SETTINGS,
  ROUTE_PROVINCE_ROUTE_IN_PROVINCE_GUILDS,
  ROUTE_PROVINCE_ROUTE_MANAGE_GUILDS,
  ROUTE_PROVINCE_ROUTE_OUT_PROVINCE_GUILDS,
  ROUTE_PROVINCE_ROUTE_IN_PROVINCE_STEWARDS,
  ROUTE_PROVINCE_SUPERVISOR_ROUTE_GUILDS,
  ROUTE_PROVINCE_SUPERVISOR_ROUTE_GUILDS_SETTINGS,
  ROUTE_PROVINCE_SUPERVISOR_ROUTE_MANAGE_GUILDS,
  ROUTE_PROVINCE_SUPERVISOR_ROUTE_STEWARDS,
  ROUTE_SUPER_ADMIN_ROUTE_GUILDS,
  ROUTE_SUPER_ADMIN_ROUTE_IN_PROVINCE_GUILDS_DISTRIBUTIONS,
  ROUTE_SUPER_ADMIN_ROUTE_IN_PROVINCE_GUILDS_REQUESTS,
  ROUTE_SUPER_ADMIN_ROUTE_GUILDS_SETTINGS,
  ROUTE_SUPER_ADMIN_ROUTE_IN_PROVINCE_GUILDS,
  ROUTE_SUPER_ADMIN_ROUTE_MANAGE_GUILDS,
  ROUTE_SUPER_ADMIN_ROUTE_OUT_PROVINCE_GUILDS,
  ROUTE_SUPER_ADMIN_ROUTE_IN_PROVINCE_STEWARDS,
  ROUTE_SUPER_ADMIN_ROUTE_IN_PROVINCE_TRUE_GUILDS,
  ROUTE_ADMINX_ROUTE_IN_PROVINCE_TRUE_GUILDS,
  ROUTE_PROVINCE_ROUTE_IN_PROVINCE_TRUE_GUILDS,
  ROUTE_SUPER_ADMIN_ROUTE_OUT_PROVINCE_TRUE_GUILDS,
  ROUTE_ADMINX_ROUTE_OUT_PROVINCE_TRUE_GUILDS,
  ROUTE_PROVINCE_ROUTE_OUT_PROVINCE_TRUE_GUILDS,
  ROUTE_SUPER_ADMIN_ROUTE_IN_PROVINCE_LEGAL_GUILDS,
  ROUTE_ADMINX_ROUTE_IN_PROVINCE_LEGAL_GUILDS,
  ROUTE_PROVINCE_ROUTE_IN_PROVINCE_LEGAL_GUILDS,
  ROUTE_SUPER_ADMIN_ROUTE_OUT_PROVINCE_LEGAL_GUILDS,
  ROUTE_ADMINX_ROUTE_OUT_PROVINCE_LEGAL_GUILDS,
  ROUTE_PROVINCE_ROUTE_OUT_PROVINCE_LEGAL_GUILDS,
  ROUTE_SLAUGHTER_ROUTE_MANAGE_GUILDS,
  ROUTE_SLAUGHTER_MANAGE_GUILDS_IN_PROVINCE,
  ROUTE_SLAUGHTER_MANAGE_GUILDS_OUT_PROVINCE,
  ROUTE_SLAUGHTER_MANAGE_GUILDS_IN_PROVINCE_GUILDS_REQUESTS,
  ROUTE_SLAUGHTER_MANAGE_GUILDS_IN_PROVINCE_TRUE,
  ROUTE_SLAUGHTER_MANAGE_GUILDS_OUT_PROVINCE_TRUE,
  ROUTE_SLAUGHTER_MANAGE_GUILDS_IN_PROVINCE_LEGAL,
  ROUTE_SLAUGHTER_MANAGE_GUILDS_OUT_PROVINCE_LEGAL,
} from "../routes/routes";
import { BackButton } from "../components/back-button/BackButton";
import { GuildsOperations } from "../features/province/components/guilds-operations/GuildsOperations";
import { ManageStewards } from "../features/province/components/manage-stewards/ManageStewards";
import { GuildsSettings } from "../features/province/components/guilds-settings/GuildsSettings";
import { GuildsOperationsCommerce } from "../features/commerce/components/guilds-operations-commerce/GuildsOperationsCommerce";
import { ManageGuildsRequests } from "../features/province/components/manage-guilds-requests/ManageGuildsRequests";
import { GuildsOperationsCityJihad } from "../features/city-jihad/components/guilds-operations-city-jihad/GuildsOperationsCityJihad";
import { GuildRoomOperations } from "../features/guild-room/components/guild-room-operations/GuildRoomOperations";
import { ManageTab } from "../features/province/components/manage-tab/ManageTab";
import { ManageGuildDistributions } from "../features/province/components/manage-guild-distributions/ManageGuildDistributions";
import { ProvinceTrueGuildsOutProvince } from "../features/province/components/province-true-guilds-out-province/ProvinceTrueGuildsOutProvince";
import { ProvinceLegalGuildsInProvince } from "../features/province/components/province-legal-guilds-in-province/ProvinceLegalGuildsInProvince";
import { ProvinceLegalGuildsOutProvince } from "../features/province/components/province-legal-guilds-out-province/ProvinceLegalGuildsOutProvince";
import { ManageGuilds } from "../features/province/components/manage-guilds/ManageGuilds";

const MANAGE_ROUTES = [
  ROUTE_PROVINCE_ROUTE_MANAGE_GUILDS,
  ROUTE_COMMERCE_ROUTE_MANAGE_GUILDS,
  ROUTE_SLAUGHTER_ROUTE_MANAGE_GUILDS,
  ROUTE_PROVINCE_SUPERVISOR_ROUTE_MANAGE_GUILDS,
  ROUTE_GUILD_ROOM_ROUTE_MANAGE_GUILDS,
  ROUTE_CITY_JIHAD_ROUTE_MANAGE_GUILDS,
  ROUTE_CITY_POULTRY_ROUTE_MANAGE_GUILDS,
  ROUTE_SUPER_ADMIN_ROUTE_MANAGE_GUILDS,
  ROUTE_ADMINX_ROUTE_MANAGE_GUILDS,
  ROUTE_SUPER_ADMIN_ROUTE_IN_PROVINCE_GUILDS,
  ROUTE_ADMINX_ROUTE_IN_PROVINCE_GUILDS,
  ROUTE_PROVINCE_ROUTE_IN_PROVINCE_GUILDS,
  ROUTE_SUPER_ADMIN_ROUTE_OUT_PROVINCE_GUILDS,
  ROUTE_ADMINX_ROUTE_OUT_PROVINCE_GUILDS,
  ROUTE_PROVINCE_ROUTE_OUT_PROVINCE_GUILDS,
  ROUTE_SLAUGHTER_MANAGE_GUILDS_IN_PROVINCE,
  ROUTE_SLAUGHTER_MANAGE_GUILDS_OUT_PROVINCE,
];

const GUILDS_OPERATIONS_ROUTES = [
  ROUTE_PROVINCE_ROUTE_MANAGE_GUILDS,
  ROUTE_SUPER_ADMIN_ROUTE_MANAGE_GUILDS,
  ROUTE_ADMINX_ROUTE_MANAGE_GUILDS,
  ROUTE_SLAUGHTER_ROUTE_MANAGE_GUILDS,
  ROUTE_SUPER_ADMIN_ROUTE_IN_PROVINCE_GUILDS,
  ROUTE_ADMINX_ROUTE_IN_PROVINCE_GUILDS,
  ROUTE_PROVINCE_ROUTE_IN_PROVINCE_GUILDS,
  ROUTE_SUPER_ADMIN_ROUTE_OUT_PROVINCE_GUILDS,
  ROUTE_ADMINX_ROUTE_OUT_PROVINCE_GUILDS,
  ROUTE_PROVINCE_ROUTE_OUT_PROVINCE_GUILDS,
  ROUTE_SLAUGHTER_MANAGE_GUILDS_IN_PROVINCE,
  ROUTE_SLAUGHTER_MANAGE_GUILDS_OUT_PROVINCE,
];

const GUILDS_OPERATIONS_COMMERCE_ROUTES = [
  ROUTE_COMMERCE_ROUTE_MANAGE_GUILDS,
  ROUTE_PROVINCE_SUPERVISOR_ROUTE_MANAGE_GUILDS,
];

const GUILDS_OPERATIONS_CITY_JIHAD_ROUTES = [
  ROUTE_CITY_JIHAD_ROUTE_MANAGE_GUILDS,
  ROUTE_CITY_POULTRY_ROUTE_MANAGE_GUILDS,
];

const MANAGE_TAB_ROUTES = [
  ROUTE_PROVINCE_ROUTE_GUILDS,
  ROUTE_SUPER_ADMIN_ROUTE_GUILDS,
  ROUTE_ADMINX_ROUTE_GUILDS,
  ROUTE_COMMERCE_ROUTE_GUILDS,
  ROUTE_GUILD_ROOM_ROUTE_GUILDS,
  ROUTE_PROVINCE_SUPERVISOR_ROUTE_GUILDS,
  ROUTE_CITY_JIHAD_ROUTE_GUILDS,
  ROUTE_CITY_POULTRY_ROUTE_GUILDS,
  ROUTE_PROVINCE_ROUTE_IN_PROVINCE_TRUE_GUILDS,
  ROUTE_ADMINX_ROUTE_IN_PROVINCE_TRUE_GUILDS,
  ROUTE_SUPER_ADMIN_ROUTE_IN_PROVINCE_TRUE_GUILDS,
];

const STEWARDS_ROUTES = [
  ROUTE_PROVINCE_ROUTE_IN_PROVINCE_STEWARDS,
  ROUTE_SUPER_ADMIN_ROUTE_IN_PROVINCE_STEWARDS,
  ROUTE_ADMINX_ROUTE_IN_PROVINCE_STEWARDS,
  ROUTE_COMMERCE_ROUTE_STEWARDS,
  ROUTE_GUILD_ROOM_ROUTE_STEWARDS,
  ROUTE_PROVINCE_SUPERVISOR_ROUTE_STEWARDS,
  ROUTE_CITY_POULTRY_ROUTE_STEWARDS,
  ROUTE_CITY_JIHAD_ROUTE_STEWARDS,
];

const GUILDS_SETTINGS_ROUTES = [
  ROUTE_PROVINCE_ROUTE_GUILDS_SETTINGS,
  ROUTE_SUPER_ADMIN_ROUTE_GUILDS_SETTINGS,
  ROUTE_ADMINX_ROUTE_GUILDS_SETTINGS,
  ROUTE_PROVINCE_SUPERVISOR_ROUTE_GUILDS_SETTINGS,
  ROUTE_COMMERCE_ROUTE_GUILDS_SETTINGS,
];

const GUILDS_REQUESTS_ROUTES = [
  ROUTE_PROVINCE_ROUTE_IN_PROVINCE_GUILDS_REQUESTS,
  ROUTE_SUPER_ADMIN_ROUTE_IN_PROVINCE_GUILDS_REQUESTS,
  ROUTE_ADMINX_ROUTE_IN_PROVINCE_GUILDS_REQUESTS,
  ROUTE_GUILD_ROOM_ROUTE_GUILDS_REQUESTS,
  ROUTE_SLAUGHTER_MANAGE_GUILDS_IN_PROVINCE_GUILDS_REQUESTS,
];

const GUILDS_DISTRIBUTIONS_ROUTES = [
  ROUTE_SUPER_ADMIN_ROUTE_IN_PROVINCE_GUILDS_DISTRIBUTIONS,
  ROUTE_ADMINX_ROUTE_IN_PROVINCE_GUILDS_DISTRIBUTIONS,
];

const TRUE_GUILDS_OUT_PROVINCE_ROUTES = [
  ROUTE_SUPER_ADMIN_ROUTE_OUT_PROVINCE_TRUE_GUILDS,
  ROUTE_ADMINX_ROUTE_OUT_PROVINCE_TRUE_GUILDS,
  ROUTE_PROVINCE_ROUTE_OUT_PROVINCE_TRUE_GUILDS,
  ROUTE_SLAUGHTER_MANAGE_GUILDS_OUT_PROVINCE_TRUE,
];

const LEGAL_GUILDS_IN_PROVINCE_ROUTES = [
  ROUTE_SUPER_ADMIN_ROUTE_IN_PROVINCE_LEGAL_GUILDS,
  ROUTE_ADMINX_ROUTE_IN_PROVINCE_LEGAL_GUILDS,
  ROUTE_PROVINCE_ROUTE_IN_PROVINCE_LEGAL_GUILDS,
  ROUTE_SLAUGHTER_MANAGE_GUILDS_IN_PROVINCE_LEGAL,
];

const LEGAL_GUILDS_OUT_PROVINCE_ROUTES = [
  ROUTE_SUPER_ADMIN_ROUTE_OUT_PROVINCE_LEGAL_GUILDS,
  ROUTE_ADMINX_ROUTE_OUT_PROVINCE_LEGAL_GUILDS,
  ROUTE_PROVINCE_ROUTE_OUT_PROVINCE_LEGAL_GUILDS,
  ROUTE_SLAUGHTER_MANAGE_GUILDS_OUT_PROVINCE_LEGAL,
];

const isPathnameIn = (pathname, routes) => routes.includes(pathname);

const isPathnameIncludes = (pathname, routes) =>
  routes.some((route) => pathname.includes(route));

const Guilds = () => {
  const { pathname } = useLocation();

  return (
    <Box display={"flex"} justifyContent="center">
      <Grid
        container
        direction="column"
        justifyContent="center"
        xs={12}
        lg={10}
      >
        {isPathnameIn(pathname, MANAGE_ROUTES) ? (
          <Grid justifyContent="center" my={SPACING.MEDIUM}>
            {isPathnameIn(pathname, GUILDS_OPERATIONS_ROUTES) && (
              <GuildsOperations />
            )}
            {isPathnameIn(pathname, GUILDS_OPERATIONS_COMMERCE_ROUTES) && (
              <GuildsOperationsCommerce />
            )}
            {isPathnameIn(pathname, GUILDS_OPERATIONS_CITY_JIHAD_ROUTES) && (
              <GuildsOperationsCityJihad />
            )}
            {pathname === ROUTE_GUILD_ROOM_ROUTE_MANAGE_GUILDS && (
              <GuildRoomOperations />
            )}
          </Grid>
        ) : (
          <BackButton />
        )}

        <Grid container justifyContent="space-between" gap={SPACING.SMALL}>
          <Grid xs={12}>
            {isPathnameIn(pathname, MANAGE_TAB_ROUTES) && <ManageTab />}

            {isPathnameIncludes(pathname, STEWARDS_ROUTES) && (
              <ManageStewards />
            )}

            {isPathnameIn(pathname, GUILDS_SETTINGS_ROUTES) && (
              <GuildsSettings />
            )}

            {isPathnameIn(pathname, GUILDS_REQUESTS_ROUTES) && (
              <ManageGuildsRequests />
            )}

            {isPathnameIn(pathname, GUILDS_DISTRIBUTIONS_ROUTES) && (
              <ManageGuildDistributions />
            )}

            {isPathnameIn(pathname, TRUE_GUILDS_OUT_PROVINCE_ROUTES) && (
              <ProvinceTrueGuildsOutProvince userType="guild" />
            )}

            {pathname === ROUTE_SLAUGHTER_MANAGE_GUILDS_IN_PROVINCE_TRUE && (
              <ManageGuilds userType="guild" />
            )}

            {isPathnameIn(pathname, LEGAL_GUILDS_IN_PROVINCE_ROUTES) && (
              <ProvinceLegalGuildsInProvince userType="guild" />
            )}

            {isPathnameIn(pathname, LEGAL_GUILDS_OUT_PROVINCE_ROUTES) && (
              <ProvinceLegalGuildsOutProvince userType="guild" />
            )}
          </Grid>
        </Grid>
      </Grid>
    </Box>
  );
};

export default Guilds;
