/**
 * DescriptionElement Component
 *
 * Description element with icon or styled text content.
 * Renders with unified wrapper styling + content.
 */

import React, { useMemo } from 'react';
import type { CSSProperties } from 'react';
import type { CanvasElement } from '../../../types/constructor';
import { resolveAssetPlaybackUrl } from '../../../lib/assetUrl';
import { getFontByKey, getFontStyle } from '../../../lib/fonts';
import { normalizePixelValue } from '../../../lib/elementStyles';

interface DescriptionElementProps {
  element: CanvasElement;
  resolveUrl?: (url: string | undefined) => string;
  className: string;
  style: CSSProperties;
}

const DescriptionElement: React.FC<DescriptionElementProps> = ({
  element,
  resolveUrl,
  className,
  style,
}) => {
  const resolve = resolveUrl ?? resolveAssetPlaybackUrl;

  // Resolve font keys to full CSS styles (including fontStretch for condensed variants)
  const titleFontStyle = useMemo(() => {
    const fontKey = element.descriptionTitleFontFamily;
    if (!fontKey) return {};
    const font = getFontByKey(fontKey);
    return font ? getFontStyle(font) : { fontFamily: fontKey };
  }, [element.descriptionTitleFontFamily]);

  const textFontStyle = useMemo(() => {
    const fontKey = element.descriptionTextFontFamily;
    if (!fontKey) return {};
    const font = getFontByKey(fontKey);
    return font ? getFontStyle(font) : { fontFamily: fontKey };
  }, [element.descriptionTextFontFamily]);

  // With icon: render image
  if (element.iconUrl) {
    const imgStyle: CSSProperties = {
      width: element.width ? '100%' : 'auto',
      height: element.height ? '100%' : 'auto',
      objectFit: 'contain',
    };

    return (
      <div className={className} style={style}>
        {/* eslint-disable-next-line @next/next/no-img-element */}
        <img
          src={resolve(element.iconUrl)}
          alt='Description'
          style={imgStyle}
          draggable={false}
        />
      </div>
    );
  }

  // Without icon: render styled text description
  // Background color is controlled via CSS Styles tab (backgroundColor property)
  // fontWeight from CSS Styles tab is applied to both title and text
  const fontWeight = element.fontWeight || 'bold';

  return (
    <div className={className} style={style}>
      <div className='p-4'>
        <p
          style={{
            fontSize:
              normalizePixelValue(element.descriptionTitleFontSize) || '24px',
            color: element.descriptionTitleColor || '#ffffff',
            fontWeight,
            ...titleFontStyle,
          }}
        >
          {element.descriptionTitle || ''}
        </p>
        {element.descriptionText && (
          <p
            style={{
              fontSize:
                normalizePixelValue(element.descriptionTextFontSize) || '16px',
              color: element.descriptionTextColor || '#ffffff',
              fontWeight,
              ...textFontStyle,
            }}
          >
            {element.descriptionText}
          </p>
        )}
      </div>
    </div>
  );
};

export default DescriptionElement;
