import ErrorManager from '@common/error/ErrorManager';
import * as React from 'react';
import { useSelector } from 'react-redux';
import { Route, Switch } from 'react-router-dom';
import 'react-tippy/dist/tippy.css';
import { ToastContainer } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
import Loader from './components/Loader';
import { useStore } from '@core/states';
import DashBoard from './pages/authenticated/Dashboard';
import OrderPage from './pages/authenticated/order-management';
import NewOrderPage from './pages/authenticated/order-management/pages/NewOrder';
import Login from './pages/guest/Login';
import navigationService from '@common/navigation/NavigationService';
import 'primeflex/primeflex.css';
import 'react-toastify/dist/ReactToastify.css';
import './assets/css/custom.css';
import './assets/css/main.scss';
import './assets/css/tooltip.css';
import GenericModal from '@components/GenericModal';
import Layout from './layout';
import TaskManagement from './pages/authenticated/task-management';
import RoleManagement from './pages/authenticated/role-management';
import UserManagement from './pages/authenticated/user-management';
import UserCreateUpdate from './pages/authenticated/user-management/pages/UserCreateUpdate';
import CreateUpdateTask from './pages/authenticated/task-management/pages/CreateUpdateTask';
import CreateUpdateRole from './pages/authenticated/role-management/pages/CreateUpdateRole';
import authState from '@core/states/AuthState';
import accountService from '@services/Account';
import NAVIGATION_PATHS from '@common/navigation/NavigationPaths';
import OrderDetail from './pages/authenticated/order-management/pages/OrderDetail';
import OrderVehicleDetail from './pages/authenticated/order-management/pages/OrderVehicleDetail';
import ChangePassword from './pages/authenticated/user-management/pages/ChangePassword';
import ResetPassword from './pages/authenticated/user-management/pages/ResetPassword';
import en from 'public/lang/en.json';
import tr from 'public/lang/tr.json';
import LocalizedStrings from '@localization/LocalizedStrings';
import CustomerManagement from './pages/authenticated/customer-management';
import CustomerCreateUpdate from './pages/authenticated/customer-management/pages/CustomerCreateUpdate';
import ProjectManagement from './pages/authenticated/project-management';
import ProjectCreateUpdate from './pages/authenticated/project-management/pages/ProjectCreateUpdate';

export interface AppMainPropTypes {
  authenticated: boolean;
  isLoading: boolean;
  location: any;
}

function AppMain({ location }: AppMainPropTypes) {
  const isAuthenticated = useSelector<any, string>(state => state.authState.authenticated);
  const modalStore = useStore('modalState');
  const { open, type, closeAction, confirmAction } = modalStore;
  const { open: loaderOpen } = useStore('loaderState');

  React.useEffect(() => {
    const lastRoute = sessionStorage.getItem('routePath');
    accountService.getInfo().then(res => {
      authState.update({ authenticated: true, roles: res.roles, fullName: res.fullName });
      let routeParameters = sessionStorage.getItem('routeParameters');

      if (routeParameters && typeof routeParameters === 'string') {
        routeParameters = JSON.parse(routeParameters);
      }
      navigationService.push(
        lastRoute || NAVIGATION_PATHS.SYSTEM_MANAGEMENT.ORDER.BASE,
        routeParameters,
      );
    });
  }, [isAuthenticated]);

  React.useEffect(() => {
    new LocalizedStrings({ EN: en, TR: tr });
  }, []);

  return (
    <>
      {loaderOpen && <Loader />}
      {open && (
        <GenericModal type={type} show={open} close={closeAction} confirmAction={confirmAction} />
      )}
      {isAuthenticated ? (
        <Layout>
          <Switch>
            <Route exact path="/dashboard" component={DashBoard} />
            <Route
              exact
              path={NAVIGATION_PATHS.SYSTEM_MANAGEMENT.ORDER.BASE}
              component={OrderPage}
            />
            <Route
              exact
              path={NAVIGATION_PATHS.SYSTEM_MANAGEMENT.ORDER.DETAIL}
              component={OrderDetail}
            />
            <Route
              exact
              path={NAVIGATION_PATHS.SYSTEM_MANAGEMENT.ORDER.VEHICLE_DETAIL}
              component={OrderVehicleDetail}
            />
            <Route
              exact
              path={NAVIGATION_PATHS.SYSTEM_MANAGEMENT.ORDER.CREATE}
              component={NewOrderPage}
            />
            <Route
              exact
              path={NAVIGATION_PATHS.SYSTEM_MANAGEMENT.TASK_MANAGEMENT.BASE}
              component={TaskManagement}
            />
            <Route
              exact
              path={NAVIGATION_PATHS.SYSTEM_MANAGEMENT.TASK_MANAGEMENT.CREATE_UPDATE}
              component={CreateUpdateTask}
            />
            <Route
              exact
              path={NAVIGATION_PATHS.SYSTEM_MANAGEMENT.USER_MANAGEMENT.BASE}
              component={UserManagement}
            />
            <Route
              exact
              path={NAVIGATION_PATHS.SYSTEM_MANAGEMENT.USER_MANAGEMENT.CREATE_UPDATE}
              component={UserCreateUpdate}
            />
            <Route
              exact
              path={NAVIGATION_PATHS.SYSTEM_MANAGEMENT.USER_MANAGEMENT.CHANGE_PASSWORD}
              component={ChangePassword}
            />
            <Route
              exact
              path={NAVIGATION_PATHS.SYSTEM_MANAGEMENT.USER_MANAGEMENT.RESET_PASSWORD}
              component={ResetPassword}
            />
            <Route
              exact
              path={NAVIGATION_PATHS.SYSTEM_MANAGEMENT.ROLE_MANAGEMENT.BASE}
              component={RoleManagement}
            />
            <Route
              exact
              path={NAVIGATION_PATHS.SYSTEM_MANAGEMENT.ROLE_MANAGEMENT.CREATE_UPDATE}
              component={CreateUpdateRole}
            />
            <Route
              exact
              path={NAVIGATION_PATHS.SYSTEM_MANAGEMENT.CUSTOMER_MANAGEMENT.BASE}
              component={CustomerManagement}
            />
            <Route
              exact
              path={NAVIGATION_PATHS.SYSTEM_MANAGEMENT.CUSTOMER_MANAGEMENT.CREATE_UPDATE}
              component={CustomerCreateUpdate}
            />
            <Route
              exact
              path={NAVIGATION_PATHS.SYSTEM_MANAGEMENT.PROJECT_MANAGEMENT.BASE}
              component={ProjectManagement}
            />
            <Route
              exact
              path={NAVIGATION_PATHS.SYSTEM_MANAGEMENT.PROJECT_MANAGEMENT.CREATE_UPDATE}
              component={ProjectCreateUpdate}
            />
          </Switch>
        </Layout>
      ) : (
        <Switch>
          <Route exact path="/" component={Login} />
          <Route exact path="/login" component={Login} />
          <Route exact path="/guest" component={Login} />
        </Switch>
      )}
      <ToastContainer />
      <ErrorManager />
    </>
  );
}

export default AppMain;
