import { useEffect, useState } from 'react';
import { useLocation } from 'react-router';
import { PATHS } from '@/shared/constants/paths';
import { useNavigate } from '@/shared/lib/hooks/use-navigate';
import { useEscrowDetailMutation } from '@/entities/transaction/api/use-escrow-detail-mutation';
import { ImportantInfoWrap } from '@/entities/transaction/ui/info-wrap/important-info-wrap';
import { EscrowInfoWrap } from '@/entities/transaction/ui/info-wrap/escrow-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 { HeaderType } from '@/entities/common/model/types';
import { 
  TransactionCategory,
  EscrowDetailParams,
  DetailResponse,
  ImportantInfo,
  EscrowInfo,
  PaymentInfo,
  TransactionInfo,
  SettlementInfo,
  InfoWrapKeys
} from '@/entities/transaction/model/types';
import {
  useSetOnBack, 
  useSetHeaderTitle,
  useSetHeaderType,
  useSetFooterMode
} from '@/widgets/sub-layout/use-sub-layout';
import { EscrowMailResendBottomSheet } from '@/entities/transaction/ui/escrow-mail-resend-bottom-sheet';
import { useEscrowMailResendMutation } from '@/entities/transaction/api/use-escrow-mail-resend-mutation';

export const EscrowDetailPage = () => {
  const { navigate } = useNavigate();
  const location = useLocation();
  
  const [amountInfo, setAmountInfo] = useState<ImportantInfo>();
  const [importantInfo, setImportantInfo] = useState<ImportantInfo>();
  const [escrowInfo, setEscrowInfo] = useState<EscrowInfo>();
  const [paymentInfo, setPaymentInfo] = useState<PaymentInfo>();
  const [transactionInfo, setTransactionInfo] = useState<TransactionInfo>();
  const [settlementInfo, setSettlementInfo] = useState<SettlementInfo>();
  const [showAmountInfo, setShowAmountInfo] = useState<boolean>(false);
  const [showImportantInfo, setShowImportantInfo] = useState<boolean>(false);
  const [showEscroInfo, setShowEscroInfo] = useState<boolean>(false);
  const [showPaymentInfo, setShowPaymentInfo] = useState<boolean>(false);
  const [showTransactionInfo, setShowTransactionInfo] = useState<boolean>(false);
  const [showSettlementInfo, setShowSettlementInfo] = useState<boolean>(false);
  const [bottomSheetOn, setBottomSheetOn] = useState<boolean>(false);

  const [orderNumber, setOrderNumber] = useState<string>();
  const [tid, setTid] = useState<string>();

  useSetHeaderTitle('에스크로 상세');
  useSetHeaderType(HeaderType.RightClose);
  useSetOnBack(() => {
    navigate(PATHS.transaction.escrow.list);
  });
  useSetFooterMode(false);

  const { mutateAsync: escrowDetail } = useEscrowDetailMutation();
  const { mutateAsync: escrowMailResend } = useEscrowMailResendMutation() 
    
  const callDetail = () => {
    let escroDetailParams: EscrowDetailParams = {
      issueNumber: location?.state.issueNumber,
    };
    escrowDetail(escroDetailParams).then((rs: DetailResponse) => {
      setImportantInfo(rs.importantInfo);
      setEscrowInfo(rs.escrowInfo);
      setPaymentInfo(rs.paymentInfo);
      setTransactionInfo(rs.transactionInfo);
      setSettlementInfo(rs.settlementInfo);

      setOrderNumber(rs.importantInfo?.ordNo);
      setTid(rs.importantInfo?.tid);
    });
  };
  useEffect(() => {
    callDetail();
  }, []);

  const onClickToShowMailResend = () => {
    setBottomSheetOn(true);
  };

  const callMailResend = () => {
    let params = {
      orderNumber: orderNumber,
      tid: tid,
    };
    escrowMailResend(params).then((rs: any) => {
      console.log(rs);
    });
  };
  
  const onClickToOpenInfo = (infoWrapKey: InfoWrapKeys) => {
    if(infoWrapKey === InfoWrapKeys.Amount){
      setShowAmountInfo(!showAmountInfo);
    }
    else if(infoWrapKey === InfoWrapKeys.Important){
      setShowImportantInfo(!showImportantInfo);
    }
    else if(infoWrapKey === InfoWrapKeys.Escrow){
      setShowEscroInfo(!showEscroInfo);
    }
    else if(infoWrapKey === InfoWrapKeys.Payment){
      setShowPaymentInfo(!showPaymentInfo);
    }
    else if(infoWrapKey === InfoWrapKeys.Transaction){
      setShowTransactionInfo(!showTransactionInfo);
    }
    else if(infoWrapKey === InfoWrapKeys.Settlement){
      setShowSettlementInfo(!showSettlementInfo);
    }
  };

  return (
    <>
      <main>
        <div className="tab-content">
          <div className="tab-pane sub active">
            <div className="option-list">
              <div className="txn-detail">
                <ImportantInfoWrap
                  transactionCategory={ TransactionCategory.Escrow }
                  importantInfo={ importantInfo }
                  
                ></ImportantInfoWrap>
                <div className="txn-divider minus"></div>
                <EscrowInfoWrap
                  transactionCategory={ TransactionCategory.Escrow }
                  importantInfo={ importantInfo }
                  isOpen={ showEscroInfo }
                  onClickToOpenInfo={ (infoWrapKey) => onClickToOpenInfo(infoWrapKey) }
                ></EscrowInfoWrap>
                <div className="txn-divider minus"></div>
                <PaymentInfoWrap
                  transactionCategory={ TransactionCategory.Escrow }
                  paymentInfo={ paymentInfo }
                  isOpen={ showPaymentInfo }
                  onClickToOpenInfo={ (infoWrapKey) => onClickToOpenInfo(infoWrapKey) }
                ></PaymentInfoWrap>
                <div className="txn-divider"></div>
                <TransactionInfoWrap
                  transactionCategory={ TransactionCategory.Escrow }
                  transactionInfo={ transactionInfo }
                  isOpen={ showTransactionInfo }
                  onClickToOpenInfo={ (infoWrapKey) => onClickToOpenInfo(infoWrapKey) }
                ></TransactionInfoWrap>
                <div className="txn-divider"></div>
                <SettlementInfoWrap
                  transactionCategory={ TransactionCategory.Escrow }
                  settlementInfo={ settlementInfo }
                  isOpen={ showSettlementInfo }
                  onClickToOpenInfo={ (infoWrapKey) => onClickToOpenInfo(infoWrapKey) }
                ></SettlementInfoWrap>
                <div className="txn-divider"></div>
              </div>
            </div>
            <div className="apply-row">
              <button 
                className="btn-50 btn-blue flex-1"
                onClick={ () => onClickToShowMailResend() }
              >메일 재발송</button>
            </div>
          </div>
        </div>
      </main>
      <EscrowMailResendBottomSheet
        setBottomSheetOn={ setBottomSheetOn }
        bottomSheetOn={ bottomSheetOn }
        callMailResend={ callMailResend }
      ></EscrowMailResendBottomSheet>
    </>
  );
};