import { __ } from '@wordpress/i18n';
import { SelectControl } from '@wordpress/components';
import ResponsiveControlV2 from '../../ResponsiveControlV2';
import { useResponsiveTabV2 } from '../../ResponsiveTabContextV2';
import PanelBodyWithSync from '../../PanelBodyWithSync';
import { getEnabledBreakpoints } from '../../../../../core/breakpoints';

const getDefaultFontWeight = () => ({
  pc: 'normal',
  md: '',
  tb: '',
  sp: ''
});

const FontWeightSettingV5 = ({ 
  attributes, 
  setAttributes, 
  title, 
  initialOpen = false, 
  opened,
  onToggle,
  onSync,
  isHoverState = false,
  supportsHover = true
}) => {
  const effectiveHoverState = supportsHover ? isHoverState : false;
  const v2Key = effectiveHoverState ? 'fontWeightV2Hover' : 'fontWeightV2';
  const responsiveAttrName = effectiveHoverState ? 'useResponsiveFontWeightHover' : 'useResponsiveFontWeight';
  const responsiveEnabled = attributes[responsiveAttrName] !== undefined ? attributes[responsiveAttrName] : false;
  const { activeTab } = useResponsiveTabV2();
  const enabledBreakpoints = getEnabledBreakpoints();

  const fontWeightData = attributes[v2Key] || getDefaultFontWeight();

  const fontWeightOptions = [
    { label: __('Default', 'zenblocks'), value: '' },
    { label: __('Normal', 'zenblocks'), value: 'normal' },
    { label: __('Bold', 'zenblocks'), value: 'bold' },
    { label: __('Light', 'zenblocks'), value: 'lighter' },
    { label: __('Bolder', 'zenblocks'), value: 'bolder' },
    { label: __('100', 'zenblocks'), value: '100' },
    { label: __('200', 'zenblocks'), value: '200' },
    { label: __('300', 'zenblocks'), value: '300' },
    { label: __('400', 'zenblocks'), value: '400' },
    { label: __('500', 'zenblocks'), value: '500' },
    { label: __('600', 'zenblocks'), value: '600' },
    { label: __('700', 'zenblocks'), value: '700' },
    { label: __('800', 'zenblocks'), value: '800' },
    { label: __('900', 'zenblocks'), value: '900' },
  ];

  const handleFontWeightChange = (value) => {
    const newFontWeight = { ...fontWeightData };
    if (!responsiveEnabled) {
      enabledBreakpoints.forEach((bp) => {
        newFontWeight[bp] = value;
      });
    } else {
      newFontWeight[activeTab] = value;
    }
    setAttributes({ [v2Key]: newFontWeight });
  };

  const handleResponsiveToggle = (value) => {
    setAttributes({ [responsiveAttrName]: value });

    const newFontWeight = { ...fontWeightData };
    const pcValue = newFontWeight.pc;
    enabledBreakpoints.forEach((bp) => {
      if (bp !== 'pc') {
        newFontWeight[bp] = pcValue;
      }
    });
    setAttributes({ [v2Key]: newFontWeight });
  };

  const renderFontWeightControls = (tabName) => {
    const currentWeight = fontWeightData[tabName] || '';

    return (
      <SelectControl
        value={currentWeight}
        options={fontWeightOptions}
        onChange={handleFontWeightChange}
        className="zenblocks-inspector-font-weight__select"
      />
    );
  };

  return (
    <PanelBodyWithSync 
      title={title || __('Font Weight', 'zenblocks')} 
      initialOpen={initialOpen}
      opened={opened}
      onToggle={onToggle}
      onSync={onSync}
      className="zenblocks-nested-panel"
      level={2}
      panelType="settings"
      panelName="font-weight"
    >
      <div className="zenblocks-inspector-font-weight">
        <ResponsiveControlV2
          initialOpen={true}
          enableResponsiveToggle={true}
          responsiveEnabled={responsiveEnabled}
          onResponsiveToggle={handleResponsiveToggle}
          attributes={attributes}
          setAttributes={setAttributes}
          syncAttributeName={responsiveAttrName}
        >
          {(tabName) => renderFontWeightControls(tabName)}
        </ResponsiveControlV2>
      </div>
    </PanelBodyWithSync>
  );
};

export const getFontWeightValue = (attributes, device = 'pc', isHoverState = false) => {
  if (!attributes) return 'normal';

  const v2Key = isHoverState ? 'fontWeightV2Hover' : 'fontWeightV2';
  const fontWeightData = attributes[v2Key];
  
  if (!fontWeightData) return 'normal';

  const enabledBreakpoints = getEnabledBreakpoints();
  const currentIndex = enabledBreakpoints.indexOf(device);
  
  for (let i = currentIndex; i >= 0; i--) {
    const bp = enabledBreakpoints[i];
    const value = fontWeightData[bp];
    if (value && value !== '') {
      return value;
    }
  }

  return 'normal';
};

export const getDirectFontWeight = (attributes, device = 'pc', isHoverState = false) => {
  if (!attributes) return undefined;

  const v2Key = isHoverState ? 'fontWeightV2Hover' : 'fontWeightV2';
  const fontWeightData = attributes[v2Key];
  
  if (!fontWeightData || !fontWeightData[device]) return undefined;

  const value = fontWeightData[device];
  return value && value !== '' ? value : undefined;
};

export default FontWeightSettingV5;
