import React, { useEffect, useState, useRef, useCallback } from 'react';
import { Navigate, Routes, Route, useLocation } from 'react-router-dom';

import URL from 'constants/url';
import CODE from 'constants/code';

//COMMON
import EgovHeader from 'components/EgovHeader';
import EgovFooter from 'components/EgovFooter';
import EgovInfoPopup from 'components/EgovInfoPopup';
import EgovError from 'components/EgovError';

import EgovMain from 'pages/main/EgovMain';
import EgovLogin from 'pages/login/EgovLogin';
import Join from 'pages/login/join/Join';

//ABOUT
import EgovAboutSite from 'pages/about/EgovAboutSite';
import EgovAboutHistory from 'pages/about/EgovAboutHistory';
import EgovAboutOrganization from 'pages/about/EgovAboutOrganization';
import EgovAboutLocation from 'pages/about/EgovAboutLocation';

//INTRO
import EgovIntroWork from 'pages/intro/EgovIntroWork';
import EgovIntroService from 'pages/intro/EgovIntroService';

//SUPPORT
import EgovSupportDownloadList from 'pages/support/download/EgovDownloadList';
import EgovSupportDownloadDetail from 'pages/support/download/EgovDownloadDetail';
import EgovSupportDownloadCreate from 'pages/support/download/EgovDownloadCreate';
import EgovSupportQnaList from 'pages/support/qna/EgovQnaList';
import EgovSupportQnaDetail from 'pages/support/qna/EgovQnaDetail';
import EgovSupportApply from 'pages/support/apply/EgovSupportApply';

//INFORM
import EgovDailyList from 'pages/inform/daily/EgovDailyList';
import EgovDailyDetail from 'pages/inform/daily/EgovDailyDetail';
import EgovWeeklyList from 'pages/inform/weekly/EgovWeeklyList';

import EgovNoticeList from 'pages/inform/notice/EgovNoticeList';
import EgovNoticeDetail from 'pages/inform/notice/EgovNoticeDetail';
import EgovNoticeEdit from 'pages/inform/notice/EgovNoticeEdit';

import EgovGalleryList from 'pages/inform/gallery/EgovGalleryList';
import EgovGalleryDetail from 'pages/inform/gallery/EgovGalleryDetail';
import EgovGalleryEdit from 'pages/inform/gallery/EgovGalleryEdit';

//ADMIN
import EgovAdminScheduleList from 'pages/admin/schedule/EgovAdminScheduleList';
import EgovAdminScheduleDetail from 'pages/admin/schedule/EgovAdminScheduleDetail';
import EgovAdminScheduleEdit from 'pages/admin/schedule/EgovAdminScheduleEdit';

import EgovAdminBoardList from 'pages/admin/board/EgovAdminBoardList';
import EgovAdminBoardEdit from 'pages/admin/board/EgovAdminBoardEdit';

import EgovAdminUsageList from 'pages/admin/usage/EgovAdminUsageList';
import EgovAdminUsageEdit from 'pages/admin/usage/EgovAdminUsageEdit';

import EgovAdminNoticeList from 'pages/admin/notice/EgovAdminNoticeList';
import EgovAdminNoticeDetail from 'pages/admin/notice/EgovAdminNoticeDetail';
import EgovAdminNoticeEdit from 'pages/admin/notice/EgovAdminNoticeEdit';

import EgovAdminGalleryList from 'pages/admin/gallery/EgovAdminGalleryList';
import EgovAdminGalleryDetail from 'pages/admin/gallery/EgovAdminGalleryDetail';
import EgovAdminGalleryEdit from 'pages/admin/gallery/EgovAdminGalleryEdit';
//사이트관리자 암호 바꾸기 기능 추가 2023.04.15(토) 김일국 추가
import EgovAdminPasswordUpdate from 'pages/admin/manager/EgovAdminPasswordUpdate';

// 관리자 - 환경설정
import AdminConfigBaseCodeMgt from 'pages/admin/config/BaseCodeMgt';                            // 관리자 - 환경설정/기본코드 관리
import AdminConfigStandardCodeMgt from 'pages/admin/config/StandardCodeMgt';                    // 관리자 - 환경설정/건설기준코드 관리
import AdminConfigCommitteeCodeMgt from 'pages/admin/config/CommitteeCodeMgt';                  // 관리자 - 환경설정/위원회 코드 관리
import AdminConfigMenuMgt from 'pages/admin/config/MenuMgt';                                    // 관리자 - 환경설정/메뉴 관리
import AdminConfigMenuAuthMgt from 'pages/admin/config/MenuAuthMgt';                            // 관리자 - 환경설정/메뉴 권한 관리
import AdminConfigAboutSiteMgt  from 'pages/admin/config/AboutSiteMgt';                         // 관리자 - 환경설정/관련 사이트 관리

// 관리자 - 사용자 현황             
import AdminUsersList from 'pages/admin/users/List';                                            // 관리자 - 사용자 현황

// 관리자 - 게시판 현황
import AdminBoardsList from 'pages/admin/boards/List';                                          // 관리자 - 게시판 현황/게시판 관리
import AdminBoardsPosts from 'pages/admin/boards/Posts';                                        // 관리자 - 게시판 현황/게시물 관리
import AdminBoardsKeywords from 'pages/admin/boards/Keywords';                                  // 관리자 - 게시판 현황/키워드 관리

// 관리자 - 건설기준 관리
import AdminStandardsReferenceCodes from 'pages/admin/standards/ReferenceCodes';                // 관리자 - 건설기준 관리/참조코드 조회
import AdminStandardsApiKeys from 'pages/admin/standards/ApiKeys';                              // 관리자 - 건설기준 관리/API KYE 관리
import AdminStandardsSimilarityCheck from 'pages/admin/standards/SimilarityCheck';              // 관리자 - 건설기준 관리/유사성 검사
import AdminStandardsInfoDisclosure from 'pages/admin/standards/InfoDisclosure';                // 관리자 - 건설기준 관리/정보공개 관리

// 관리자 - 컨텐츠 관리
import AdminContentsSurvey from 'pages/admin/contents/Survey';                                   // 관리자 - 컨텐츠 관리/설문 관리
import AdminContentsPopUp from 'pages/admin/contents/PopUp';                                     // 관리자 - 컨텐츠 관리/팝업 관리
import AdminContentsPopUpWriter from 'pages/admin/contents/PopUp/PopupWriter';                   // 관리자 - 컨텐츠 관리/팝업 관리/팝업 추가 또는 수정
import AdminContentsStandardResearch from 'pages/admin/contents/StandardResearch';               // 관리자 - 컨텐츠 관리/건설기준연구 관리
import AdminContentsTextMessages from 'pages/admin/contents/TextMessages';                       // 관리자 - 컨텐츠 관리/문자 발송

// 관리자 - 위원회 관리
import AdminCommitteeProgressStatus from 'pages/admin/committee/ProgressStatus';                // 관리자 - 위원회 관리/진행현황 관리
import AdminCommitteeSchedules from 'pages/admin/committee/Schedules';                          // 관리자 - 위원회 관리/위원회 일정 관리
import AdminCommitteeSchedulesDetail from 'pages/admin/committee/Schedules/Detail';             // 관리자 - 위원회 관리/위원회 일정 관리/일정관리상세
import AdminCommitteeSchedulesEdit from 'pages/admin/committee/Schedules/Edit';                 // 관리자 - 위원회 관리/위원회 일정 관리/일정관리생성 또는 수정

// 관리자 - 로그 관리
import AdminLogsMenuAccessInfo from 'pages/admin/logs/MenuAccessInfo';                           // 관리자 - 위원회 관리/메뉴별 접속현황
import AdminLogsUserConnections from 'pages/admin/logs/UserConnections';                         // 관리자 - 위원회 관리/사용자 접속현황
import AdminLogsPrivacyLogs from 'pages/admin/logs/PrivacyLogs';                                 // 관리자 - 위원회 관리/개인정보 로그
import AdminLogsFileDownloadStatus from 'pages/admin/logs/FileDownloadStatus';                   // 관리자 - 위원회 관리/파일 다운현황


//건설기준코드
import CodeViewer from 'pages/standardCode/viewer';

import * as EgovNet from 'api/egovFetch'; // jwt토큰 위조 검사 때문에 추가
import initPage from 'js/ui';
import StandardCodeList from "../pages/standardCode/StandardCodeList";

const RootRoutes = () => {
  //useLocation객체를 이용하여 정규표현식을 사용한 /admin/~ 으로 시작하는 경로와 비교에 사용(아래 1줄) */}
  const location = useLocation();

  //리액트에서 사이트관리자에 접근하는 토큰값 위변조 방지용으로 서버에서 비교하는 함수 추가
  const jwtAuthentication = useCallback(() => {
    console.group("jwtAuthentication");
    console.log("[Start] jwtAuthentication ------------------------------");

    const jwtAuthURL = "/auth/token-check";
    let requestOptions = {
      method: "POST",
    };

    EgovNet.requestFetch(
        jwtAuthURL,
        requestOptions,
        (resp) => {
          if (resp === false) {
            setMounted(false);
          } else {
            setMounted(true); // 이 값으로 true 일 때만 페이지를 렌더링이 되는 변수 사용.
          }
        }
    );

    console.log("------------------------------jwtAuthentication [End]");
    console.groupEnd("jwtAuthentication");
  }, []);

  //시스템관리 메뉴인 /admin/으로 시작하는 URL은 모두 로그인이 필요하도록 코드추가(아래)
  const isMounted = useRef(false); // 아래 로그인 이동 부분이 2번 실행되지 않도록 즉, 마운트 될 때만 실행되도록 변수 생성
  const [mounted, setMounted] = useState(false);// 컴포넌트 최초 마운트 후 리렌더링 전 로그인 페이지로 이동하는 조건으로 사용

  useEffect(() => {
	if (!isMounted.current) { // 컴포넌트 최초 마운트 시 페이지 진입 전(렌더링 전) 실행
		isMounted.current = true; // 이 값으로 true 일 때만 페이지를 렌더링이 되는 변수 사용.
		setMounted(true); // 이 값으로 true 일 때만 페이지를 렌더링이 되는 변수 사용.
		const regex = /^(\/admin\/)+(.)*$/; //정규표현식 사용: /admin/~ 으로 시작하는 경로 모두 포함
		if(regex.test(location.pathname)) {
			setMounted(false); // 이 값으로 true 일 때만 페이지를 렌더링이 되는 변수 사용. 기본은 숨기기
			jwtAuthentication(); // 이 함수에서 관리자단 인증여부 확인 후 렌더링 처리
		}
	}
  },[jwtAuthentication, location, mounted]); // location 경로와 페이지 마운트상태가 변경 될 때 업데이트 후 리렌더링

  if(mounted) { // 인증 없이 시스템관리 URL로 접근할 때 렌더링 되는 것을 방지하는 조건추가. 
	  return (
	      <Routes>
	        <Route path={URL.ERROR} element={<EgovError />} />
	        <Route path="*" element={<SecondRoutes/>} />
	      </Routes>
	  )
  }
}

const SecondRoutes = () => {

  const [loginVO, setLoginVO] = useState({});

  //useRef객체를 사용하여 페이지 마운트 된 후 ui.js를 로딩 하도록 변경 코드 추가(아래)
  const isMounted = useRef(false); // 아래 로그인 이동 부분이 2번 실행되지 않도록 즉, 마운트 될 때만 실행되도록 변수 생성
  useEffect(() => {
    if (!isMounted.current) { // 컴포넌트 최초 마운트 시 페이지 진입 전(렌더링 전) 실행
		isMounted.current = true; // 이 값으로 true 일 때만 페이지를 렌더링이 되는 변수 사용.
	}else{
		initPage();
	}
  },[]);
  
  return (
    <>
      <EgovHeader loginUser={loginVO} onChangeLogin={(user) => setLoginVO(user)} />
      <Routes>
        {/* MAIN */}
        <Route path={URL.MAIN} element={<EgovMain />} />

        {/* LOGIN */}
        <Route path={URL.LOGIN} element={<EgovLogin onChangeLogin={(user) => setLoginVO(user)}/>}/>
        {/*{JOIN}*/}
        <Route path={URL.JOIN} element={<Join />} />

        {/* ERROR */}
        <Route path={URL.ERROR} element={<EgovError />} />

        {/* ABOUT */}
        <Route path={URL.ABOUT} element={<Navigate to={URL.ABOUT_SITE} />} />
        <Route path={URL.ABOUT_SITE} element={<EgovAboutSite />} />
        <Route path={URL.ABOUT_HISTORY} element={<EgovAboutHistory />} />
        <Route path={URL.ABOUT_ORGANIZATION} element={<EgovAboutOrganization />} />
        <Route path={URL.ABOUT_LOCATION} element={<EgovAboutLocation />} />

        {/* INTRO */}
        <Route path={URL.INTRO} element={<Navigate to={URL.INTRO_WORKS} />} />
        <Route path={URL.INTRO_WORKS} element={<EgovIntroWork />} />
        <Route path={URL.INTRO_SERVICE} element={<EgovIntroService />} />

        {/* SUPPORT */}
        <Route path={URL.SUPPORT} element={<Navigate to={URL.SUPPORT_DOWNLOAD} />} />

        <Route path={URL.SUPPORT_DOWNLOAD} element={<EgovSupportDownloadList />} />
        <Route path={URL.SUPPORT_DOWNLOAD_DETAIL} element={<EgovSupportDownloadDetail />} />
        <Route path={URL.SUPPORT_DOWNLOAD_CREATE} element={<EgovSupportDownloadCreate />} />

        <Route path={URL.SUPPORT_QNA} element={<EgovSupportQnaList />} />
        <Route path={URL.SUPPORT_QNA_DETAIL} element={<EgovSupportQnaDetail />} />

        <Route path={URL.SUPPORT_APPLY} element={<EgovSupportApply />} />

        {/* INFORM */}
        <Route path={URL.INFORM} element={<Navigate to={URL.INFORM_DAILY} />} />

        <Route path={URL.INFORM_DAILY} element={<EgovDailyList />} />
        <Route path={URL.INFORM_DAILY_DETAIL} element={<EgovDailyDetail />} />
        <Route path={URL.INFORM_WEEKLY} element={<EgovWeeklyList />} />
        <Route path={URL.INFORM_WEEKLY_DETAIL} element={<EgovDailyDetail />} />

        <Route path={URL.INFORM_NOTICE} element={<EgovNoticeList />} />
        <Route path={URL.INFORM_NOTICE_DETAIL} element={<EgovNoticeDetail />} />
        <Route path={URL.INFORM_NOTICE_CREATE} element={<EgovNoticeEdit mode={CODE.MODE_CREATE} />} />
        <Route path={URL.INFORM_NOTICE_MODIFY} element={<EgovNoticeEdit mode={CODE.MODE_MODIFY} />} />
        <Route path={URL.INFORM_NOTICE_REPLY} element={<EgovNoticeEdit mode={CODE.MODE_REPLY} />} />

        <Route path={URL.INFORM_GALLERY} element={<EgovGalleryList />} />
        <Route path={URL.INFORM_GALLERY_DETAIL} element={<EgovGalleryDetail />} />
        <Route path={URL.INFORM_GALLERY_CREATE} element={<EgovGalleryEdit mode={CODE.MODE_CREATE} />} />
        <Route path={URL.INFORM_GALLERY_MODIFY} element={<EgovGalleryEdit mode={CODE.MODE_MODIFY} />} />
        <Route path={URL.INFORM_GALLERY_REPLY} element={<EgovGalleryEdit mode={CODE.MODE_REPLY} />} />

        {/* ADMIN */}
        <Route path={URL.ADMIN} element={<Navigate to={URL.ADMIN_SCHEDULE} />} />
        <Route path={URL.ADMIN_SCHEDULE} element={<EgovAdminScheduleList />} />
        <Route path={URL.ADMIN_SCHEDULE_DETAIL} element={<EgovAdminScheduleDetail />} />
        <Route path={URL.ADMIN_SCHEDULE_CREATE} element={<EgovAdminScheduleEdit mode={CODE.MODE_CREATE} />} />
        <Route path={URL.ADMIN_SCHEDULE_MODIFY} element={<EgovAdminScheduleEdit mode={CODE.MODE_MODIFY} />} />

        <Route path={URL.ADMIN_BOARD} element={<EgovAdminBoardList />} />
        <Route path={URL.ADMIN_BOARD_CREATE} element={<EgovAdminBoardEdit mode={CODE.MODE_CREATE} />} />
        <Route path={URL.ADMIN_BOARD_MODIFY} element={<EgovAdminBoardEdit mode={CODE.MODE_MODIFY} />} />

        <Route path={URL.ADMIN_USAGE} element={<EgovAdminUsageList />} />
        <Route path={URL.ADMIN_USAGE_CREATE} element={<EgovAdminUsageEdit mode={CODE.MODE_CREATE} />} />
        <Route path={URL.ADMIN_USAGE_MODIFY} element={<EgovAdminUsageEdit mode={CODE.MODE_MODIFY} />} />

        <Route path={URL.ADMIN_NOTICE} element={<EgovAdminNoticeList />} />
        <Route path={URL.ADMIN_NOTICE_DETAIL} element={<EgovAdminNoticeDetail />} />
        <Route path={URL.ADMIN_NOTICE_CREATE} element={<EgovAdminNoticeEdit mode={CODE.MODE_CREATE} />} />
        <Route path={URL.ADMIN_NOTICE_MODIFY} element={<EgovAdminNoticeEdit mode={CODE.MODE_MODIFY} />} />
        <Route path={URL.ADMIN_NOTICE_REPLY} element={<EgovAdminNoticeEdit mode={CODE.MODE_REPLY} />} />

        <Route path={URL.ADMIN_GALLERY} element={<EgovAdminGalleryList />} />
        <Route path={URL.ADMIN_GALLERY_DETAIL} element={<EgovAdminGalleryDetail />} />
        <Route path={URL.ADMIN_GALLERY_CREATE} element={<EgovAdminGalleryEdit mode={CODE.MODE_CREATE} />} />
        <Route path={URL.ADMIN_GALLERY_MODIFY} element={<EgovAdminGalleryEdit mode={CODE.MODE_MODIFY} />} />
        <Route path={URL.ADMIN_GALLERY_REPLY} element={<EgovAdminGalleryEdit mode={CODE.MODE_REPLY} />} />
        {/* 사이트관리자 암호 바꾸기 기능 추가 2023.04.15(토) 김일국 */}
        <Route path={URL.ADMIN_MANAGER} element={<EgovAdminPasswordUpdate />} />

        {/* 관리자 - 환경설정 */}
        <Route path={URL.ADMIN_BASE_CODE} element={<AdminConfigBaseCodeMgt />} />
        <Route path={URL.ADMIN_STANDARD_CODE} element={<AdminConfigStandardCodeMgt />} />
        <Route path={URL.ADMIN_COMMITTEE_CODE} element={<AdminConfigCommitteeCodeMgt />} />
        <Route path={URL.ADMIN_MENU} element={<AdminConfigMenuMgt />} />
        <Route path={URL.ADMIN_MENU_AUTH} element={<AdminConfigMenuAuthMgt />} />
        <Route path={URL.ADMIN_ABOUT_SITE} element={<AdminConfigAboutSiteMgt />} />

        {/* 관리자 - 사용자 현황 */}
        <Route path={URL.ADMIN__USERS__LIST} element={<AdminUsersList />} />

        {/* 관리자 - 게시판 현황 */}
        <Route path={URL.ADMIN__BOARDS__LIST} element={<AdminBoardsList />} />
        <Route path={URL.ADMIN__BOARDS__POSTS} element={<AdminBoardsPosts />} />
        <Route path={URL.ADMIN__BOARDS__KEYWORDS} element={<AdminBoardsKeywords />} />
        
        {/* 관리자 - 건설기준 관리 */}
        <Route path={URL.ADMIN__STANDARDS__REFERENCE_CODES} element={<AdminStandardsReferenceCodes />} />
        <Route path={URL.ADMIN__STANDARDS__API_KYES} element={<AdminStandardsApiKeys />} />
        <Route path={URL.ADMIN__STANDARDS__SIMILARITY_CHECK} element={<AdminStandardsSimilarityCheck />} />
        <Route path={URL.ADMIN__STANDARDS__INFO_DISCLOSURE} element={<AdminStandardsInfoDisclosure />} />

        {/* 관리자 - 컨텐츠 관리 */}
        <Route path={URL.ADMIN__CONTENTS__SURVEY} element={<AdminContentsSurvey />} />
        <Route path={URL.ADMIN__CONTENTS__POP_UP} element={<AdminContentsPopUp />} />
        <Route path={URL.ADMIN__CONTENTS__POP_UP__CREATE} element={<AdminContentsPopUpWriter mode={CODE.MODE_CREATE} />}  />
        <Route path={URL.ADMIN__CONTENTS__POP_UP__MODIFY} element={<AdminContentsPopUpWriter mode={CODE.MODE_MODIFY} />}  />
        <Route path={URL.ADMIN__CONTENTS__STANDARDS_RESEARCH} element={<AdminContentsStandardResearch />} />
        <Route path={URL.ADMIN__CONTENTS__TEXT_MESSAGES} element={<AdminContentsTextMessages />} />

        {/* 관리자 - 위원회 관리 */}
        <Route path={URL.ADMIN__COMMITTEE__PROGRESS_STATUS} element={<AdminCommitteeProgressStatus />} />
        <Route path={URL.ADMIN__COMMITTEE__SCHEDULES} element={<AdminCommitteeSchedules />} />
        <Route path={URL.ADMIN__COMMITTEE__SCHEDULES__DETAIL} element={<AdminCommitteeSchedulesDetail />} />
        <Route path={URL.ADMIN__COMMITTEE__SCHEDULES__CREATE} element={<AdminCommitteeSchedulesEdit mode={CODE.MODE_CREATE} />}  />
        <Route path={URL.ADMIN__COMMITTEE__SCHEDULES__MODIFY} element={<AdminCommitteeSchedulesEdit mode={CODE.MODE_MODIFY} />}  />



        {/* 관리자 - 로그 관리 */}
        <Route path={URL.ADMIN__LOGS__MENU_ACCESS_INFO} element={<AdminLogsMenuAccessInfo />} />
        <Route path={URL.ADMIN__LOGS__USER_CONNECTIONS} element={<AdminLogsUserConnections />} />
        <Route path={URL.ADMIN__LOGS__PRIVACY_LOGS} element={<AdminLogsPrivacyLogs />} />
        <Route path={URL.ADMIN__LOGS__FILE_DOWNLOAD_STATUS} element={<AdminLogsFileDownloadStatus />} />



        {/*기준코드 뷰어*/}
        <Route path={URL.STANDARD_CODE_VIEWER} element={<CodeViewer mode={CODE.MODE_READ} />} />
        <Route path={URL.STANDARD_CODE_VIEWER_LINK} element={<CodeViewer mode={CODE.MODE_READ} />} />

        {/*기준코드리스트*/}
        <Route path={URL.STANDARD_CODE_LIST} element={<StandardCodeList />} />

      </Routes>
      <EgovFooter />
      <EgovInfoPopup />
      
    </>
  )
  
}


export default RootRoutes;