import { ChangeEvent, useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { PATHS } from '@/shared/constants/paths';
import { useNavigate } from '@/shared/lib/hooks/use-navigate';
import { HeaderType } from '@/entities/common/model/types';
import { useFaqListMutation } from '@/entities/support/api/use-faq-list-mutation';
import { DEFAULT_PAGE_PARAM } from '@/entities/common/model/constant';
import { FaqItem } from '@/entities/support/model/types';
import { SupportFaqItem } from '@/entities/support/ui/faq-item';
import { 
	useSetHeaderTitle,
	useSetHeaderType,
	useSetFooterMode,
  useSetOnBack
} from '@/widgets/sub-layout/use-sub-layout';

export const FaqListPage = () => {
  const { navigate } = useNavigate();
  const { t } = useTranslation();

  const [pageParam, setPageParam] = useState(DEFAULT_PAGE_PARAM);
  const [searchValue, setSearchValue] = useState<string>('');
  const [selectedCategory, setSelectedCategory] = useState<string>('all');
  const [resultList, setResultList] = useState<Array<FaqItem>>([]);

  useSetHeaderTitle(t('support.faq.title'));
  useSetHeaderType(HeaderType.LeftArrow);
  useSetFooterMode(true);
  useSetOnBack(() => {
    navigate(PATHS.home);
  });

  const { mutateAsync: faqList } = useFaqListMutation();
  const callList = () => {
    let listParams = {
      category: selectedCategory,
      searchValue: searchValue,
      ...{page: pageParam}
    };
    
    faqList(listParams).then((rs) => {
      console.log(rs)
      setResultList(rs.content);
    });
  };

  const onClickToAction = () => {
    // Remove focus from active element
    if (document.activeElement instanceof HTMLElement) {
      document.activeElement.blur();
    }
    callList();
  };

  const getFaqList = () => {
    let rs = [];
    for(let i=0;i<resultList.length;i++){
      rs.push(
        <SupportFaqItem
          key={ `key-support-faq-item-${i}` }
          sortNo={ resultList[i]?.sortNo }
          seq={ resultList[i]?.seq }
          category={ resultList[i]?.category }
          categoryName={ resultList[i]?.categoryName }
          title={ resultList[i]?.title }
          contents={ resultList[i]?.contents }
        ></SupportFaqItem>
      )
    }
    return rs;
  };

  const onClickToNavigation = () => {
    navigate(PATHS.support.qna.list);
  };

  useEffect(() => {
    callList();
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [selectedCategory]);

  return (
    <>
      <main>
        <div className="tab-content">
          <div className="tab-pane sub active">
            <div className="faq-section">
              <div className="faq-controlls">
                <div className="faq-search">
                  <span 
                    className="ic16 search" 
                    aria-hidden="true"
                    onClick={ () => onClickToAction() }
                  ></span>
                  <input
                    type="text"
                    placeholder={t('support.faq.searchPlaceholder')}
                    value={ searchValue }
                    onChange={ (e: ChangeEvent<HTMLInputElement>) => setSearchValue(e.target.value) }
                    onKeyDown={ (e: React.KeyboardEvent<HTMLInputElement>) => {
                      if (e.key === 'Enter' && !e.nativeEvent.isComposing) {
                        onClickToAction();
                      }
                    }}
                  />
                </div>
                <div className="faq-filter">
                  <select
                      className="flex-1"
                      value={selectedCategory}
                      onChange={(e: ChangeEvent<HTMLSelectElement>) => setSelectedCategory(e.target.value)}>
                    <option value="all">{t('support.faq.categories.all')}</option>
                    <option value="01">{t('support.faq.categories.01')}</option>
                    <option value="02">{t('support.faq.categories.02')}</option>
                    <option value="03">{t('support.faq.categories.03')}</option>
                    <option value="04">{t('support.faq.categories.04')}</option>
                    <option value="05">{t('support.faq.categories.05')}</option>
                    <option value="99">{t('support.faq.categories.99')}</option>
                  </select>
                </div>
              </div>
              <div className="faq-list">
              { getFaqList() }
              </div>
            </div>
            <div className="apply-row bottom-padding">
              <button 
                className="btn-50 btn-blue flex-1"
                onClick={ () => onClickToNavigation() }
              >{t('support.faq.inquiryButton')}</button>
            </div>
          </div>
        </div>
      </main>
    </>
  );
};