import { useTranslation } from 'react-i18next';
import { SectionTitleArrow } from '@/entities/common/ui/section-title-arrow';
import { Escrow, SectionKeys } from '../../model/types';
import SlideDown from 'react-slidedown';
import 'react-slidedown/lib/slidedown.css';
import { useEffect, useState } from 'react';

export interface EscrowSectionProps extends Escrow {
  type: SectionKeys;
  title?: string;
  openChild: SectionKeys | null;
  setOpenChild: (openChild: SectionKeys | null) => void;
};

export const EscrowSection = ({
  type,
  title,
  companyName,
  businessRegistrationNumber,
  escrowStatus,
  address,
  merchantUrl,
  serviceRegistrationNumber,
  openChild,
  setOpenChild
}: EscrowSectionProps) => {
  const { t } = useTranslation();

  const [isOpen, setIsOpen] = useState<boolean>(false);

  const openSection = () => {
    const status = !isOpen;
    setIsOpen(status);
    if(!!status){
      setOpenChild(type);  
    }
    else {
      setOpenChild(null)
    }
  };

  useEffect(() => {
    if(!!openChild && openChild !== type){
    setIsOpen(false);
    }
  }, [openChild]);
  
  return (
    <>
      <div className="section">
        <div className="section-title"
          onClick={ () => openSection() }
        >
          { title } <SectionTitleArrow isOpen={ isOpen }></SectionTitleArrow>
        </div>
        <SlideDown className={'my-dropdown-slidedown'}>
        { isOpen &&
          <ul className="kv-list">
            <li className="kv-row">
              <span className="k">{t('merchant.companyName')}</span>
              <span className="v">{ companyName }</span>
            </li>
            <li className="kv-row">
              <span className="k">{t('merchant.businessRegistrationNumber')}</span>
              <span className="v">{ businessRegistrationNumber }</span>
            </li>
            <li className="kv-row">
              <span className="k">{t('merchant.nicecrowJoin')}</span>
              <span className="v">{ escrowStatus }</span>
            </li>
            <li className="kv-row">
              <span className="k">{t('merchant.location')}</span>
              <span className="v">{ address }</span>
            </li>
            <li className="kv-row">
              <span className="k">{t('merchant.url')}</span>
              <span className="v">{ merchantUrl }</span>
            </li>
            <li className="kv-row">
              <span className="k">{t('merchant.serviceRegistrationNumber')}</span>
              <span className="v">{ serviceRegistrationNumber }</span>
            </li>
          </ul>
        }
        </SlideDown> 
      </div>
    </>
  );
};