import { useEffect, useState } from 'react';
import { useLocation } from 'react-router';
import { PATHS } from '@/shared/constants/paths';
import { Dialog } from '@/shared/ui/dialogs/dialog';
import { overlay } from 'overlay-kit';
import { useNavigate } from '@/shared/lib/hooks/use-navigate';
import { useAllTransactionDetailMutation } from '@/entities/transaction/api/use-all-transaction-detail-mutation';
import { AmountInfoWrap } from '@/entities/transaction/ui/info-wrap/amount-info-wrap';
import { ImportantInfoWrap } from '@/entities/transaction/ui/info-wrap/important-info-wrap';
import { PaymentInfoWrap } from '@/entities/transaction/ui/info-wrap/payment-info-wrap';
import { TransactionInfoWrap } from '@/entities/transaction/ui/info-wrap/transaction-info-wrap';
import { SettlementInfoWrap } from '@/entities/transaction/ui/info-wrap/settlement-info-wrap';
import { PartCancelInfoWrap } from '@/entities/transaction/ui/info-wrap/part-cancel-info-wrap';
import { HeaderType } from '@/entities/common/model/types';
import { 
  TransactionCategory,
  AllTransactionDetailParams,
  DetailResponse,
  AmountInfo,
  ImportantInfo,
  PaymentInfo,
  TransactionInfo,
  SettlementInfo,
  PartCancelInfo,
  InfoWrapKeys
} from '@/entities/transaction/model/types';
import {
  useSetOnBack, 
  useSetHeaderTitle,
  useSetHeaderType,
  useSetFooterMode
} from '@/widgets/sub-layout/use-sub-layout';

export const AllTransactionDetailPage = () => {
  const { navigate } = useNavigate();
  const location = useLocation();
  const tid = location.state.tid;
  const serviceCode = location.state.serviceCode;
  
  const [amountInfo, setAmountInfo] = useState<AmountInfo>();
  const [importantInfo, setImportantInfo] = useState<ImportantInfo>();
  const [paymentInfo, setPaymentInfo] = useState<PaymentInfo>();
  const [transactionInfo, setTransactionInfo] = useState<TransactionInfo>();
  const [settlementInfo, setSettlementInfo] = useState<SettlementInfo>();
  const [partCancelInfo, setPartCancelInfo] = useState<PartCancelInfo>();
  const [showAmountInfo, setShowAmountInfo] = useState<boolean>(false);
  const [showPaymentInfo, setShowPaymentInfo] = useState<boolean>(false);
  const [showTransactionInfo, setShowTransactionInfo] = useState<boolean>(false);
  const [showSettlementInfo, setShowSettlementInfo] = useState<boolean>(false);
  const [showPartCancelInfo, setShowPartCancelInfo] = useState<boolean>(false);

  useSetHeaderTitle('거래내역 상세');
  useSetHeaderType(HeaderType.RightClose);
  useSetOnBack(() => {
    navigate(PATHS.transaction.allTransaction.list);
  });
  useSetFooterMode(false);

  const { mutateAsync: allTransactionDetail } = useAllTransactionDetailMutation();
  
  const callDetail = () => {
    let allTransactionDetailParams: AllTransactionDetailParams = {
      svcCd: 'st',
      tid: tid
    };
    allTransactionDetail(allTransactionDetailParams).then((rs: DetailResponse) => {
      setAmountInfo(rs.amountInfo);
      setImportantInfo(rs.importantInfo);
      setPaymentInfo(rs.paymentInfo);
      setTransactionInfo(rs.transactionInfo);
      setSettlementInfo(rs.settlementInfo);
      setPartCancelInfo(rs.partCancelInfo);
    });
  };
  useEffect(() => {
    callDetail();
  }, []);

  const onClickToNavigate = (path: string) => {
    let timeout = setTimeout(() => {
      clearTimeout(timeout);
      navigate(PATHS.transaction.allTransaction.cancel, {
        state: {
          tid: tid
        }
      });
    }, 10)
  };

  const onClickToCancel = () => {
    let msg = '거래를 취소하시겠습니까?';
    
    overlay.open(({ 
      isOpen,
      close,
      unmount
    }) => {
      return (
        <Dialog
          afterLeave={ unmount }
          open={ isOpen }
          onClose={ close }
          onConfirmClick={ () => onClickToNavigate(PATHS.transaction.allTransaction.cancel) }
          message={ msg }
          buttonLabel={['취소', '확인']}
        />
      );
    });
  };

  const onClickToOpenInfo = (infoWrapKey: InfoWrapKeys) => {
    if(infoWrapKey === InfoWrapKeys.Amount){
      setShowAmountInfo(!showAmountInfo);
    }
    else if(infoWrapKey === InfoWrapKeys.Payment){
      setShowPaymentInfo(!showPaymentInfo);
    }
    else if(infoWrapKey === InfoWrapKeys.Transaction){
      setShowTransactionInfo(!showTransactionInfo);
    }
    else if(infoWrapKey === InfoWrapKeys.Settlement){
      setShowSettlementInfo(!showSettlementInfo);
    }
    else if(infoWrapKey === InfoWrapKeys.PartCancel){
      setShowPartCancelInfo(!showPartCancelInfo);
    }
  };

  return (
    <>
      <main>
        <div className="tab-content">
          <div className="tab-pane sub active">
            <div className="option-list">
              <div className="txn-detail">
                <AmountInfoWrap
                  transactionCategory={ TransactionCategory.AllTransaction }
                  amountInfo={ amountInfo }
                  isOpen={ showAmountInfo }
                  tid={ tid } 
                  serviceCode={ serviceCode }
                  onClickToOpenInfo={ (infoWrapKey) => onClickToOpenInfo(infoWrapKey) }
                ></AmountInfoWrap>
                <div className="txn-divider"></div>
                <ImportantInfoWrap
                  transactionCategory={ TransactionCategory.AllTransaction }
                  importantInfo={ importantInfo }
                  serviceCode={ serviceCode }
                ></ImportantInfoWrap>
                <div className="txn-divider"></div>
                <PaymentInfoWrap
                  transactionCategory={ TransactionCategory.AllTransaction }
                  paymentInfo={ paymentInfo }
                  serviceCode={ serviceCode }
                  isOpen={ showPaymentInfo }
                  onClickToOpenInfo={ (infoWrapKey) => onClickToOpenInfo(infoWrapKey) }
                ></PaymentInfoWrap>
                <div className="txn-divider"></div>
                <TransactionInfoWrap
                  transactionCategory={ TransactionCategory.AllTransaction }
                  transactionInfo={ transactionInfo }
                  serviceCode={ serviceCode }
                  isOpen={ showTransactionInfo }
                  onClickToOpenInfo={ (infoWrapKey) => onClickToOpenInfo(infoWrapKey) }
                ></TransactionInfoWrap>
                <div className="txn-divider"></div>
                <SettlementInfoWrap
                  transactionCategory={ TransactionCategory.AllTransaction }
                  settlementInfo={ settlementInfo }
                  serviceCode={ serviceCode }
                  isOpen={ showSettlementInfo }
                  onClickToOpenInfo={ (infoWrapKey) => onClickToOpenInfo(infoWrapKey) }
                ></SettlementInfoWrap>
                <div className="txn-divider"></div>
                <PartCancelInfoWrap
                  transactionCategory={ TransactionCategory.AllTransaction }
                  partCancelInfo={ partCancelInfo }
                  serviceCode={ serviceCode }
                  isOpen={ showPartCancelInfo }
                  onClickToOpenInfo={ (infoWrapKey) => onClickToOpenInfo(infoWrapKey) }
                ></PartCancelInfoWrap>
              </div>
            </div>
            <div className="apply-row">
              <button 
                className="btn-50 btn-blue flex-1"
                onClick={ () => onClickToCancel() }
              >거래 취소</button>
            </div>
          </div>
        </div>
      </main>
    </>
  );
};