import React, { CSSProperties } from 'react';
import { useNode, UserComponent } from '@craftjs/core';
import { cssPropsToString } from '../../utils/style-helpers';

interface DividerProps {
  color?: string;
  thickness?: string;
  style?: CSSProperties;
}

export const Divider: UserComponent<DividerProps> = ({
  color = '#e4e4e7',
  thickness = '1px',
  style = {},
}) => {
  const {
    connectors: { connect, drag },
    selected,
  } = useNode((node) => ({
    selected: node.events.selected,
  }));

  return (
    <hr
      ref={(ref: HTMLHRElement | null): void => { if (ref) connect(drag(ref)); }}
      style={{
        border: 'none',
        borderTop: `${thickness} solid ${color}`,
        margin: '16px 0',
        outline: selected ? '2px solid #3b82f6' : 'none',
        ...style,
      }}
    />
  );
};

/* ---------- Settings panel ---------- */

const DividerSettings: React.FC = () => {
  const { actions: { setProp }, props } = useNode((node) => ({
    props: node.data.props as DividerProps,
  }));

  const colorPresets = ['#e4e4e7', '#d4d4d8', '#a1a1aa', '#3f3f46', '#18181b', '#3b82f6', '#ef4444', '#10b981'];
  const thicknessPresets = ['1px', '2px', '3px', '4px', '6px'];

  return (
    <div style={{ padding: '12px', display: 'flex', flexDirection: 'column', gap: '14px' }}>
      <div>
        <label style={{ fontSize: 11, color: '#a1a1aa', display: 'block', marginBottom: 6 }}>Color</label>
        <div style={{ display: 'flex', gap: 4, flexWrap: 'wrap' }}>
          {colorPresets.map((c) => (
            <button
              key={c}
              onClick={() => setProp((p: DividerProps) => { p.color = c; })}
              style={{
                width: 24, height: 24, borderRadius: 4, border: '1px solid #3f3f46',
                backgroundColor: c, cursor: 'pointer',
                outline: props.color === c ? '2px solid #3b82f6' : 'none',
                outlineOffset: 1,
              }}
            />
          ))}
        </div>
      </div>

      <div>
        <label style={{ fontSize: 11, color: '#a1a1aa', display: 'block', marginBottom: 6 }}>Thickness</label>
        <div style={{ display: 'flex', gap: 4, flexWrap: 'wrap' }}>
          {thicknessPresets.map((t) => (
            <button
              key={t}
              onClick={() => setProp((p: DividerProps) => { p.thickness = t; })}
              style={{
                padding: '4px 10px', fontSize: 11, borderRadius: 4, cursor: 'pointer',
                border: '1px solid #3f3f46',
                background: props.thickness === t ? '#3b82f6' : '#27272a',
                color: '#e4e4e7',
              }}
            >
              {t}
            </button>
          ))}
        </div>
      </div>
    </div>
  );
};

/* ---------- Craft config ---------- */

Divider.craft = {
  displayName: 'Divider',
  props: {
    color: '#e4e4e7',
    thickness: '1px',
    style: {},
  },
  rules: {
    canDrag: () => true,
    canMoveIn: () => false,
    canMoveOut: () => true,
  },
  related: {
    settings: DividerSettings,
  },
};

/* ---------- HTML export ---------- */

(Divider as any).toHtml = (props: DividerProps, _childrenHtml: string) => {
  const styleStr = cssPropsToString({
    border: 'none',
    borderTop: `${props.thickness || '1px'} solid ${props.color || '#e4e4e7'}`,
    margin: '16px 0',
    ...props.style,
  });
  return { html: `<hr${styleStr ? ` style="${styleStr}"` : ''} />` };
};
