import moment from 'moment';
import { SectionTitleArrow } from '@/entities/common/ui/section-title-arrow';
import { InfoSectionKeys, InfoSectionProps, TransactionCategory } from '../../model/types';
import { NumericFormat } from 'react-number-format';
import { SlideDown } from 'react-slidedown';
import 'react-slidedown/lib/slidedown.css';

export const PaymentInfoSection = ({
  transactionCategory,
  paymentInfo,
  serviceCode,
  isOpen,
  onClickToOpenInfo
}: InfoSectionProps) => {

  const subItems: Record<string, Record<string, string>> = {
    approvalPurchase: {name: '승인매입', type: 'string'},
    approvalRejectCount: {name: '승인반송(횟수)', type: 'number', addString: '건'},
    approvalRepurchaseCount: {name: '승인재매입(횟수)', type: 'number', addString: '건'},
    approvalVAN: {name: '승인VAN', type: 'string'},
    cancelPurchase: {name: '취소매입', type: 'string'},
    cancelRejectCount: {name: '취소반송', type: 'number', addString: '건'},
    cancelRepurchaseCount: {name: '취소재매입', type: 'number', addString: '건'},
    purchaseVan: {name: '매입VAN', type: 'string'},
    purchaseCompany: {name: '매입사(발급사)', type: 'string'},
    cardNo: {name: '카드번호', type: 'string'},
    approvalNo: {name: '승인번호', type: 'string'},
    installmentMonth: {name: '할부기간', type: 'string', addString: '개월'},
    authentication: {name: '인증', type: 'string'},

    joinType: {name: '유형', type: 'string'},
    bankName: {name: '은행명', type: 'string'},
    accountNo: {name: '계좌번호', type: 'string'},
    refundCompleteDate: {name: '환불완료일', type: 'string'},
    customerId: {name: '고객ID', type: 'string'},
    
    culturelandId: {name: '컬처랜드ID', type: 'string'},

    partner: {name: '제휴사', type: 'string'},
    cpid: {name: 'CPID', type: 'string'},
    goodsCategory: {name: '상품구분', type: 'string'},
    cellphoneNo: {name: '휴대폰번호', type: 'string'},

    giftCardNumber: {name: '상품권번호', type: 'string'},

    depositBankName: {name: '입금금융기관명', type: 'string'},
    depositorName: {name: '입금자명', type: 'string'},
    depositDeadline: {name: '입금기한', type: 'date'},
    depositDate: {name: '입금일', type: 'date'},
    refundScheduleDate: {name: '환불예정일', type: 'date'},
    refundBankName: {name: '환불은행명', type: 'string'},
    refundAccountNo: {name: '환불계좌번호', type: 'string'},
    accountHolder: {name: '예금주', type: 'string'},
  };

  const openSubItems: Record<string, Array<string>> = {
    // 신용카드
    '01': ['approvalPurchase', 'approvalRejectCount', 'approvalRepurchaseCount', 
      'approvalVAN', 'cancelPurchase', 'cancelRejectCount', 'cancelRepurchaseCount',
      'purchaseVan', 'purchaseCompany', 'cardNo', 'approvalNo',
      'installmentMonth', 'authentication'],
    // 계좌이체
    '02': ['joinType', 'bankName', 'accountNo'],
    // 가상계좌
    '03': ['bankName', 'accountNo', 'depositBankName', 'depositorName',
      'depositDeadline', 'depositDate', 'refundScheduleDate',
      'refundBankName', 'refundAccountNo', 'accountHolder'],
    // 휴대폰
    '05': ['refundCompleteDate', 'partner', 'cpid', 'goodsCategory', 'cellphoneNo'],
    // 문화상품권
    '14': ['culturelandId'],
    // SSG머니
    '21': ['giftCardNumber'],
    // SSG은행계좌
    '24': [],
    // 계좌간편결제
    '26': ['bankName', 'refundCompleteDate', 'accountHolder', 'joinType', 'customerId'],
    // 티머니페이
    '31': ['cardNo', 'approvalNo', 'cpid'],
  };

  const checkValue = (val: any) => {
    return (!!val || val === 0);
  };
  let newPaymentInfo: Record<string, any> | undefined = paymentInfo;
  const subLi = () => {
    let rs = [];

    if(!!newPaymentInfo && !!serviceCode && !!openSubItems[serviceCode]){
      for(let i=0;i<openSubItems[serviceCode].length;i++){
        let k = openSubItems[serviceCode][i];
        if(!!k){
          rs.push(
            <li 
              key={`key-important-item-${i}`}
              className="kv-row"
            >
              <span className="k">·&nbsp;&nbsp;{ subItems[k]?.name }</span>
              <span className="v">
              { (checkValue(newPaymentInfo[k]) && subItems[k]?.type === 'string') &&
                (k === 'installmentMonth' && newPaymentInfo[k] === '00') &&
                  '일시불'
              }
              { (checkValue(newPaymentInfo[k]) && subItems[k]?.type === 'string') &&
                (k === 'installmentMonth' && newPaymentInfo[k] !== '00') &&
                newPaymentInfo[k] + (subItems[k]?.addString || '')
              }
              { (checkValue(newPaymentInfo[k]) && subItems[k]?.type === 'string') &&
                ( k!== 'installmentMonth' ) &&
                newPaymentInfo[k] + (subItems[k]?.addString || '')
              }
              { (checkValue(newPaymentInfo[k]) && subItems[k]?.type === 'number') &&
                <NumericFormat
                  value={ newPaymentInfo[k] } 
                  thousandSeparator
                  displayType="text"
                  suffix={ subItems[k]?.addString }
                ></NumericFormat>
              }
              { (checkValue(newPaymentInfo[k]) && subItems[k]?.type === 'date') &&
                moment(newPaymentInfo[k]).format('YYYY.MM.DD')
              }
              </span>
            </li>
          );
        }
      }
    }
    return rs;
  };

  const onClickToSetOpenInfo = () => {
    if(!!onClickToOpenInfo){
      onClickToOpenInfo(InfoSectionKeys.Payment);
    }
  };
  
  return (
    <>
      <div className="txn-section">
        <div 
          className="section-title with-toggle"
          onClick={ () => onClickToSetOpenInfo() }
        >
          결제 정보 <SectionTitleArrow isOpen={ isOpen }></SectionTitleArrow>
        </div>
        <SlideDown className={'my-dropdown-slidedown'}>
        { !!isOpen &&
          <ul className="kv-list">
            { (transactionCategory === TransactionCategory.AllTransaction) && 
              subLi()
            }
            { (transactionCategory === TransactionCategory.Escrow) && 
              <>
                <li className="kv-row">
                  <span className="k">·&nbsp;&nbsp;승인매입</span>
                  <span className="v">{  }</span>
                </li>
                <li className="kv-row">
                  <span className="k">·&nbsp;&nbsp;승인반송(횟수)</span>
                  <span className="v">{  }</span>
                </li>
                <li className="kv-row">
                  <span className="k">·&nbsp;&nbsp;승인재매입(횟수)</span>
                  <span className="v">{  }</span>
                </li>
                <li className="kv-row">
                  <span className="k">·&nbsp;&nbsp;승인VAN</span>
                  <span className="v">{  }</span>
                </li>
                <li className="kv-row">
                  <span className="k">·&nbsp;&nbsp;취소매입</span>
                  <span className="v">{  }</span>
                </li>
                <li className="kv-row">
                  <span className="k">·&nbsp;&nbsp;취소반송</span>
                  <span className="v">{  }</span>
                </li>
                <li className="kv-row">
                  <span className="k">·&nbsp;&nbsp;취소재매입</span>
                  <span className="v">{  }</span>
                </li>
                <li className="kv-row">
                  <span className="k">·&nbsp;&nbsp;매입VAN</span>
                  <span className="v">{  }</span>
                </li>
                <li className="kv-row">
                  <span className="k">·&nbsp;&nbsp;매입사(발급사)</span>
                  <span className="v">{  }</span>
                </li>
                <li className="kv-row">
                  <span className="k">·&nbsp;&nbsp;카드번호</span>
                  <span className="v">{  }</span>
                </li>
                <li className="kv-row">
                  <span className="k">·&nbsp;&nbsp;승인번호</span>
                  <span className="v">{  }</span>
                </li>
                <li className="kv-row">
                  <span className="k">·&nbsp;&nbsp;할부기간</span>
                  <span className="v">{  }</span>
                </li>
                <li className="kv-row">
                  <span className="k">·&nbsp;&nbsp;인증</span>
                  <span className="v">{  }</span>
                </li>
                <li className="kv-row">
                  <span className="k">·&nbsp;&nbsp;유형</span>
                  <span className="v">{  }</span>
                </li>
                <li className="kv-row">
                  <span className="k">·&nbsp;&nbsp;은행명</span>
                  <span className="v">{  }</span>
                </li>
                <li className="kv-row">
                  <span className="k">·&nbsp;&nbsp;계좌번호</span>
                  <span className="v">{  }</span>
                </li>
                <li className="kv-row">
                  <span className="k">·&nbsp;&nbsp;입금금융기관명</span>
                  <span className="v">{  }</span>
                </li>
                <li className="kv-row">
                  <span className="k">·&nbsp;&nbsp;입금자명</span>
                  <span className="v">{  }</span>
                </li>
                <li className="kv-row">
                  <span className="k">·&nbsp;&nbsp;입금기한</span>
                  <span className="v">{  }</span>
                </li>
                <li className="kv-row">
                  <span className="k">·&nbsp;&nbsp;입금일</span>
                  <span className="v">{  }</span>
                </li>
                <li className="kv-row">
                  <span className="k">·&nbsp;&nbsp;환불예정일</span>
                  <span className="v">{  }</span>
                </li>
                <li className="kv-row">
                  <span className="k">·&nbsp;&nbsp;환불은행명</span>
                  <span className="v">{  }</span>
                </li>
                <li className="kv-row">
                  <span className="k">·&nbsp;&nbsp;환불계좌번호</span>
                  <span className="v">{  }</span>
                </li>
                <li className="kv-row">
                  <span className="k">·&nbsp;&nbsp;예금주</span>
                  <span className="v">{  }</span>
                </li>
              </>
            }
          </ul>
        }
        </SlideDown>
      </div>
    </>
  )
};