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

export const SettlementInfoSection = ({
  transactionCategory,
  settlementInfo,
  serviceCode,
  isOpen,
  onClickToOpenInfo
}: InfoSectionProps) => {
  const { t } = useTranslation();

  const subItems: Record<string, Record<string, string>> = {
    approvalSettlementDate: {name: t('transaction.fields.approvalSettlementDate'), type: 'date'},
    approvalSettlementAmount: {name: t('transaction.fields.approvalSettlementAmount'), type: 'number'},
    cancelSettlementDate: {name: t('transaction.fields.cancelSettlementDate'), type: 'date'},
    cancelSettlementAmount: {name: t('transaction.fields.cancelSettlementAmount'), type: 'number'},
  };

  const openSubItems: Record<string, Array<string>> = {
    // 신용카드
    '01': ['approvalSettlementDate', 'approvalSettlementAmount', 
      'cancelSettlementDate', 'cancelSettlementAmount'],
    // 계좌이체
    '02': ['approvalSettlementDate', 'approvalSettlementAmount', 
      'cancelSettlementDate', 'cancelSettlementAmount'],
    // 가상계좌
    '03': ['approvalSettlementDate', 'approvalSettlementAmount', 
      'cancelSettlementDate', 'cancelSettlementAmount'],
    // 휴대폰
    '05': ['approvalSettlementDate', 'approvalSettlementAmount', 
      'cancelSettlementDate', 'cancelSettlementAmount'],
    // 문화상품권
    '14': ['approvalSettlementDate', 'approvalSettlementAmount', 
      'cancelSettlementDate', 'cancelSettlementAmount'],
    // SSG머니
    '21': ['approvalSettlementDate', 'approvalSettlementAmount', 
      'cancelSettlementDate', 'cancelSettlementAmount'],
    // SSG은행계좌
    '24': ['approvalSettlementDate', 'approvalSettlementAmount', 
      'cancelSettlementDate', 'cancelSettlementAmount'],
    // 계좌간편결제
    '26': ['approvalSettlementDate', 'approvalSettlementAmount', 
      'cancelSettlementDate', 'cancelSettlementAmount'],
    // 티머니페이
    '31': ['approvalSettlementDate', 'approvalSettlementAmount', 
      'cancelSettlementDate', 'cancelSettlementAmount'],
  };

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

    if(!!newSettlementInfo && !!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(newSettlementInfo[k]) && subItems[k]?.type === 'string') &&
                newSettlementInfo[k] 
              }
              { (checkValue(newSettlementInfo[k]) && subItems[k]?.type === 'number') &&
                <NumericFormat
                  value={ newSettlementInfo[k] } 
                  thousandSeparator
                  displayType="text"
                  suffix='원'
                ></NumericFormat>
              }
              { (checkValue(newSettlementInfo[k]) && subItems[k]?.type === 'date') &&
                moment(newSettlementInfo[k]).format('YYYY.MM.DD')
              }
              </span>
            </li>
          );
        }
      }
    }
    return rs;
  };

  const onClickToSetOpenInfo = () => {
    if(!!onClickToOpenInfo){
      onClickToOpenInfo(InfoSectionKeys.Settlement);
    }
  };
  
  return (
    <>
      <div className="txn-section">
        <div
          className="section-title with-toggle"
          onClick={ () => onClickToSetOpenInfo() }
        >
          {t('transaction.sections.settlementInfo')} <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;{t('transaction.fields.approvalSettlementDate')}</span>
                <span className="v">{ settlementInfo?.approvalSettlementDate? moment(settlementInfo?.approvalSettlementDate).format('YYYY.MM.DD'): '' }</span>
              </li>
              <li className="kv-row">
                <span className="k">·&nbsp;&nbsp;{t('transaction.fields.approvalSettlementAmount')}</span>
                <span className="v">
                  <NumericFormat
                    value={ settlementInfo?.approvalSettlementAmount }
                    thousandSeparator
                    displayType="text"
                    suffix='원'
                  ></NumericFormat>
                </span>
              </li>
              <li className="kv-row">
                <span className="k">·&nbsp;&nbsp;{t('transaction.fields.cancelSettlementDate')}</span>
                <span className="v">{ settlementInfo?.cancelSettlementDate? moment(settlementInfo?.cancelSettlementDate).format('YYYY.MM.DD'): '' }</span>
              </li>
              <li className="kv-row">
                <span className="k">·&nbsp;&nbsp;{t('transaction.fields.cancelSettlementAmount')}</span>
                <span className="v">
                  <NumericFormat
                    value={ settlementInfo?.cancelSettlementAmount }
                    thousandSeparator
                    displayType="text"
                    suffix='원'
                  ></NumericFormat>
                </span>
              </li>
            </>
          }
          </ul>
        }
        </SlideDown>
      </div>
    </>
  )
};