"use client";

import React from "react";
import { LayoutRendererProps } from "../BaseLayoutRenderer";

/**
 * split 컴포넌트
 */
export interface SplitLayoutProps extends LayoutRendererProps {
  renderer: any; // SplitLayoutRenderer 타입
}

export const SplitLayout: React.FC<SplitLayoutProps> = ({
  layout,
  isDesignMode = false,
  isSelected = false,
  onClick,
  className = "",
  renderer,
  onZoneComponentDrop,
  onZoneClick,
  ...props
}) => {
  if (!layout.layoutConfig.split) {
    return (
      <div className="error-layout flex items-center justify-center rounded border-2 border-destructive/30 bg-destructive/10 p-4">
        <div className="text-center text-destructive">
          <div className="font-medium">split 설정이 없습니다.</div>
          <div className="mt-1 text-sm">layoutConfig.split가 필요합니다.</div>
        </div>
      </div>
    );
  }

  const splitConfig = layout.layoutConfig.split;
  const containerStyle = renderer.getLayoutContainerStyle();

  // split 컨테이너 스타일
  const splitStyle: React.CSSProperties = {
    ...containerStyle,
    display: "flex",
    flexDirection: splitConfig.direction || "row", // 기본값: 가로 분할
    height: "100%",
    width: "100%",
    gap: "8px",
    padding: "8px",
  };

  // 디자인 모드 스타일
  if (isDesignMode) {
    splitStyle.border = isSelected ? "2px solid #3b82f6" : "1px solid #e2e8f0";
    splitStyle.borderRadius = "8px";
  }

  // DOM props만 추출 (React DOM에서 인식하는 props만)
  const {
    children: propsChildren,
    onUpdateLayout,
    onSelectComponent,
    isDesignMode: _isDesignMode,
    allComponents,
    onComponentDrop,
    onDragStart,
    onDragEnd,
    selectedScreen, // DOM에 전달하지 않도록 제외
    ...domProps
  } = props;

  return (
    <>
      <style jsx>{`
        .force-split-layout {
          display: flex !important;
          height: ${layout.size?.height ? `${layout.size.height}px` : "100%"} !important;
          min-height: ${layout.size?.height ? `${layout.size.height}px` : "200px"} !important;
          width: 100% !important;
        }
      `}</style>
      <div
        className={`split-layout force-split-layout ${isDesignMode ? "design-mode" : ""} ${className}`}
        style={splitStyle}
        onClick={onClick}
        draggable={isDesignMode}
        onDragStart={onDragStart}
        onDragEnd={onDragEnd}
        {...domProps}
      >
        {layout.zones.map((zone: any, index: number) => {
          const zoneChildren = renderer.getZoneChildren(zone.id);

          // 레이아웃 전체 높이 기준으로 존 높이 계산
          const layoutHeight = layout.size?.height || 400; // 기본값 400px
          const defaultZoneHeight =
            splitConfig.direction === "vertical"
              ? Math.floor(layoutHeight / layout.zones.length) - 32 // 세로 분할시 존 개수로 나눔
              : layoutHeight - 32; // 가로 분할시 전체 높이 사용

          // 분할 레이아웃 존 스타일
          const zoneStyle: React.CSSProperties = {
            flex: 1, // 동일한 크기로 분할
            // 카드 레이아웃처럼 항상 명확한 경계 표시
            backgroundColor: "white",
            border: "1px solid #e5e7eb",
            borderRadius: "8px",
            padding: "16px",
            boxShadow: "0 1px 3px 0 rgba(0, 0, 0, 0.1)",
            // 존의 높이: 개별 설정이 있으면 우선, 없으면 부모 높이를 100% 따라가도록
            height: zone.size?.height
              ? typeof zone.size.height === "number"
                ? `${zone.size.height}px`
                : zone.size.height
              : "100%", // 분할 영역의 높이를 100% 따라감
            minHeight: zone.size?.minHeight
              ? typeof zone.size.minHeight === "number"
                ? `${zone.size.minHeight}px`
                : zone.size.minHeight
              : "100px",
            maxHeight: zone.size?.maxHeight
              ? typeof zone.size.maxHeight === "number"
                ? `${zone.size.maxHeight}px`
                : zone.size.maxHeight
              : "none",
            position: "relative",
            transition: "all 0.2s ease",
            margin: "4px",
            overflow: "hidden",
            display: "flex",
            flexDirection: "column",
          };

          return (
            <div
              key={zone.id}
              style={zoneStyle}
              className="split-zone"
              onMouseEnter={(e) => {
                e.currentTarget.style.borderColor = "#3b82f6";
                e.currentTarget.style.boxShadow =
                  "0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 0 0 2px rgba(59, 130, 246, 0.1)";
              }}
              onMouseLeave={(e) => {
                e.currentTarget.style.borderColor = "#e5e7eb";
                e.currentTarget.style.boxShadow = "0 1px 3px 0 rgba(0, 0, 0, 0.1)";
              }}
            >
              {/* 존 라벨 */}
              {isDesignMode && (
                <div
                  className="zone-label"
                  style={{
                    position: "absolute",
                    top: "-2px",
                    left: "8px",
                    backgroundColor: "#3b82f6",
                    color: "white",
                    fontSize: "10px",
                    padding: "2px 6px",
                    borderRadius: "0 0 4px 4px",
                    fontWeight: "500",
                    zIndex: 10,
                  }}
                >
                  {zone.name || zone.id}
                </div>
              )}

              {/* 존 내용 */}
              <div
                style={{ paddingTop: isDesignMode ? "16px" : "0", flex: 1, display: "flex", flexDirection: "column" }}
              >
                {renderer.renderZone(zone, zoneChildren, {
                  style: {
                    border: "none",
                    backgroundColor: "transparent",
                    flex: 1,
                    minHeight: "0",
                  },
                  className: "split-zone-content",
                })}
              </div>
            </div>
          );
        })}

        {/* 디자인 모드에서 빈 영역 표시 */}
        {isDesignMode && layout.zones.length === 0 && (
          <div
            className="empty-split-container"
            style={{
              flex: 1,
              border: "2px dashed #cbd5e1",
              borderRadius: "8px",
              backgroundColor: "rgba(148, 163, 184, 0.05)",
              display: "flex",
              alignItems: "center",
              justifyContent: "center",
              fontSize: "14px",
              color: "#64748b",
              minHeight: "100px",
              padding: "20px",
              textAlign: "center",
            }}
          >
            split에 존을 추가하세요
          </div>
        )}
      </div>
    </>
  );
};
