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

interface ButtonLinkProps {
  text?: string;
  href?: string;
  target?: '_self' | '_blank';
  style?: CSSProperties;
}

export const ButtonLink: UserComponent<ButtonLinkProps> = ({
  text = 'Click Me',
  href = '#',
  target = '_self',
  style = {},
}) => {
  const {
    connectors: { connect, drag },
    selected,
  } = useNode((node) => ({
    selected: node.events.selected,
  }));

  return (
    <a
      ref={(ref: HTMLAnchorElement | null) => { if (ref) connect(drag(ref)); }}
      href={href}
      target={target}
      onClick={(e) => {
        // Prevent navigation inside editor
        e.preventDefault();
      }}
      style={{
        display: 'inline-block',
        textDecoration: 'none',
        cursor: 'pointer',
        outline: selected ? '2px solid #3b82f6' : 'none',
        ...style,
      }}
    >
      {text}
    </a>
  );
};

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

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

  const colorPresets = [
    { bg: '#3b82f6', color: '#ffffff', label: 'Blue' },
    { bg: '#10b981', color: '#ffffff', label: 'Green' },
    { bg: '#ef4444', color: '#ffffff', label: 'Red' },
    { bg: '#f59e0b', color: '#18181b', label: 'Amber' },
    { bg: '#8b5cf6', color: '#ffffff', label: 'Purple' },
    { bg: '#18181b', color: '#ffffff', label: 'Dark' },
    { bg: '#ffffff', color: '#18181b', label: 'White' },
    { bg: 'transparent', color: '#3b82f6', label: 'Ghost' },
  ];
  const radiusPresets = ['0px', '4px', '8px', '12px', '9999px'];
  const paddingPresets = ['8px 16px', '10px 20px', '12px 24px', '14px 32px', '16px 40px'];

  return (
    <div style={{ padding: '12px', display: 'flex', flexDirection: 'column', gap: '14px' }}>
      <div>
        <label style={{ fontSize: 11, color: '#a1a1aa', display: 'block', marginBottom: 6 }}>Button Text</label>
        <input
          type="text"
          value={props.text || ''}
          onChange={(e) => setProp((p: ButtonLinkProps) => { p.text = e.target.value; })}
          style={{ width: '100%', padding: '4px 8px', background: '#27272a', color: '#e4e4e7', border: '1px solid #3f3f46', borderRadius: 4, fontSize: 12 }}
        />
      </div>

      <div>
        <label style={{ fontSize: 11, color: '#a1a1aa', display: 'block', marginBottom: 6 }}>Link URL</label>
        <input
          type="text"
          value={props.href || ''}
          onChange={(e) => setProp((p: ButtonLinkProps) => { p.href = e.target.value; })}
          placeholder="https://..."
          style={{ width: '100%', padding: '4px 8px', background: '#27272a', color: '#e4e4e7', border: '1px solid #3f3f46', borderRadius: 4, fontSize: 12 }}
        />
      </div>

      <div>
        <label style={{ fontSize: 11, color: '#a1a1aa', display: 'block', marginBottom: 6 }}>Target</label>
        <div style={{ display: 'flex', gap: 4 }}>
          {(['_self', '_blank'] as const).map((t) => (
            <button
              key={t}
              onClick={() => setProp((p: ButtonLinkProps) => { p.target = t; })}
              style={{
                padding: '4px 10px', fontSize: 11, borderRadius: 4, cursor: 'pointer',
                border: '1px solid #3f3f46',
                background: props.target === t ? '#3b82f6' : '#27272a',
                color: '#e4e4e7',
              }}
            >
              {t === '_self' ? 'Same Tab' : 'New Tab'}
            </button>
          ))}
        </div>
      </div>

      <div>
        <label style={{ fontSize: 11, color: '#a1a1aa', display: 'block', marginBottom: 6 }}>Button Color</label>
        <div style={{ display: 'flex', gap: 4, flexWrap: 'wrap' }}>
          {colorPresets.map((preset) => (
            <button
              key={preset.label}
              onClick={() => setProp((p: ButtonLinkProps) => {
                p.style = {
                  ...p.style,
                  backgroundColor: preset.bg,
                  color: preset.color,
                  border: preset.bg === 'transparent' ? `1px solid ${preset.color}` : 'none',
                };
              })}
              title={preset.label}
              style={{
                width: 24, height: 24, borderRadius: 4,
                border: preset.bg === 'transparent' ? `2px solid ${preset.color}` : '1px solid #3f3f46',
                backgroundColor: preset.bg, cursor: 'pointer',
                outline: props.style?.backgroundColor === preset.bg ? '2px solid #3b82f6' : 'none',
                outlineOffset: 1,
              }}
            />
          ))}
        </div>
      </div>

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

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

      <div>
        <label style={{ fontSize: 11, color: '#a1a1aa', display: 'block', marginBottom: 6 }}>Font Size</label>
        <input
          type="text"
          placeholder="e.g. 16px"
          value={(props.style?.fontSize as string) || ''}
          onChange={(e) => setProp((p: ButtonLinkProps) => { p.style = { ...p.style, fontSize: e.target.value }; })}
          style={{ width: '100%', padding: '4px 8px', background: '#27272a', color: '#e4e4e7', border: '1px solid #3f3f46', borderRadius: 4, fontSize: 11 }}
        />
      </div>
    </div>
  );
};

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

ButtonLink.craft = {
  displayName: 'Button',
  props: {
    text: 'Click Me',
    href: '#',
    target: '_self',
    style: {
      backgroundColor: '#3b82f6',
      color: '#ffffff',
      padding: '12px 24px',
      borderRadius: '8px',
      fontWeight: '600',
      fontSize: '16px',
      border: 'none',
    },
  },
  rules: {
    canDrag: () => true,
    canMoveIn: () => false,
    canMoveOut: () => true,
  },
  related: {
    settings: ButtonLinkSettings,
  },
};

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

(ButtonLink as any).toHtml = (props: ButtonLinkProps, _childrenHtml: string) => {
  const styleStr = cssPropsToString({
    display: 'inline-block',
    textDecoration: 'none',
    ...props.style,
  });
  const escapedText = (props.text || '').replace(/</g, '&lt;').replace(/>/g, '&gt;');
  const targetAttr = props.target === '_blank' ? ' target="_blank" rel="noopener noreferrer"' : '';
  return {
    html: `<a href="${props.href || '#'}"${targetAttr}${styleStr ? ` style="${styleStr}"` : ''}>${escapedText}</a>`,
  };
};
