/**
 * EmailPreviewContent Component
 *
 * Renders the full email preview with browser bar, email header, email from, and email content
 * Based on preview-body structure from PreviewChrome component
 */

import React from 'react';
import PropTypes from 'prop-types';
import CapRow from '@capillarytech/cap-ui-library/CapRow';
import CapIcon from '@capillarytech/cap-ui-library/CapIcon';
import CapLabel from '@capillarytech/cap-ui-library/CapLabel';
import CapDivider from '@capillarytech/cap-ui-library/CapDivider';
import CapSpin from '@capillarytech/cap-ui-library/CapSpin';
import { FormattedMessage } from 'react-intl';
import { removeLinksFromHtmlContent } from '../../../utils/common';
import messages from '../messages';
import {
  DESKTOP, MOBILE, TABLET, deviceAspectRatio
} from '../constants';

const EmailPreviewContent = ({
  content,
  subject,
  device,
  isUpdating,
  error,
  formatMessage,
}) => {
  // Clean HTML content by removing links
  const cleanContent = content ? removeLinksFromHtmlContent(content) : '';

  // Get device-specific iframe dimensions
  const iframeDimensions = deviceAspectRatio[device || DESKTOP](subject);

  // Render common email preview structure (browser bar, header, from)
  const renderEmailStructure = (contentSection) => (
    <CapRow useLegacy className={`preview-body ${device || DESKTOP}`}>
      {/* Browser Bar */}
      <CapRow type="flex" className="browser-bar">
        <CapRow type="flex" className="browser-controls">
          <CapIcon type="chevron-left" />
          <CapIcon type="chevron-right" />
          <CapIcon className="refresh-icon" type="refreshCircle" size="s" />
        </CapRow>
        <CapRow type="flex" className="address-bar">
          <CapLabel type="label3" className="address-bar-label">
            <FormattedMessage {...messages.browserAddressBar} />
          </CapLabel>
          <CapIcon className="browser-address-bar-icon" type="star" size="s" />
        </CapRow>
      </CapRow>

      {/* Divider */}
      <CapDivider className="preview-divider" />

      {/* Email Header */}
      <CapRow type="flex" className="email-header">
        <CapIcon type="arrow-left" className="back-arrow" />
        <CapLabel type="label17" className="email-subject">{subject || ''}</CapLabel>
        <CapRow type="flex" className="email-meta">
          <span><FormattedMessage {...messages.timeAgo} /></span>
          <CapRow useLegacy className="dots" />
          <CapRow useLegacy className="dots" />
          <CapRow useLegacy className="dots" />
        </CapRow>
      </CapRow>

      {/* Email From */}
      <CapRow type="flex" className="email-from">
        <CapRow useLegacy className="sender-avatar" />
        <CapRow useLegacy className="sender-info">
          <CapRow useLegacy className="sender-name"><FormattedMessage {...messages.senderName} /></CapRow>
          <CapRow useLegacy className="recipient-info">{formatMessage(messages.toMe)}</CapRow>
        </CapRow>
      </CapRow>

      {/* Email Content - Varies based on state */}
      <CapRow useLegacy className="email-content">
        {contentSection}
      </CapRow>
    </CapRow>
  );

  // Determine content section based on state
  let contentSection;
  if (isUpdating) {
    // Loading state
    contentSection = (
      <CapRow className="loading-container">
        <CapSpin size="large" />
        <CapRow useLegacy className="loading-text">
          {formatMessage(messages.updatingPreview)}
        </CapRow>
      </CapRow>
    );
  } else if (error) {
    // Error state
    contentSection = (
      <CapRow className="error-container">
        <CapIcon type="exclamation-circle" style={{ fontSize: '48px', color: '#ff4d4f' }} />
        <CapRow useLegacy className="error-text">
          {formatMessage(messages.previewError)}
        </CapRow>
        <CapRow useLegacy className="error-message">{error}</CapRow>
      </CapRow>
    );
  } else if (cleanContent) {
    // Normal state with content
    contentSection = (
      <iframe
        srcDoc={cleanContent}
        title="Email Preview"
        width={iframeDimensions.width}
        height={iframeDimensions.height}
        frameBorder="0"
      />
    );
  } else {
    // No content state
    contentSection = (
      <CapRow className="no-content">
        {formatMessage(messages.noContent)}
      </CapRow>
    );
  }

  return renderEmailStructure(contentSection);
};

EmailPreviewContent.propTypes = {
  content: PropTypes.string,
  subject: PropTypes.string,
  device: PropTypes.oneOf([DESKTOP, MOBILE, TABLET]),
  isUpdating: PropTypes.bool,
  error: PropTypes.string,
  formatMessage: PropTypes.func.isRequired,
};

EmailPreviewContent.defaultProps = {
  content: '',
  subject: '',
  device: DESKTOP,
  isUpdating: false,
  error: null,
};

export default EmailPreviewContent;
