<template>
  <div class="form-builder-canvas">
    <div v-if="formComponents.length === 0" class="flex flex-col items-center justify-center h-full py-16 text-gray-400">
      <Icon name="material-symbols:layers-outline" class="w-16 h-16 mb-4 text-gray-300" />
      <div class="text-center max-w-md">
        <p class="text-base font-medium text-gray-600 mb-2">Start Building Your Form</p>
        <p class="text-sm text-gray-500 mb-4">Drag components from the left sidebar or click on them to add to your form</p>
        
        <div class="bg-blue-50 border border-blue-200 rounded-lg p-4 text-left">
          <div class="flex items-start">
            <Icon name="heroicons:light-bulb" class="w-5 h-5 text-blue-600 mr-2 mt-0.5 flex-shrink-0" />
            <div>
              <p class="text-sm font-medium text-blue-800 mb-1">Quick Start Tips:</p>
              <ul class="text-xs text-blue-700 space-y-1">
                <li>• Add form fields from the sidebar</li>
                <li>• Click any field to configure its settings</li>
                <li>• Use the settings button to customize fields</li>
              </ul>
            </div>
          </div>
        </div>
      </div>
    </div>
    
    <div v-else class="grid-container">
      <draggable
        v-model="componentList"
        group="form-components"
        item-key="id"
        handle=".drag-handle"
        ghost-class="ghost"
        animation="0"
        class="draggable-container"
        @end="onDragEnd"
        @start="onDragStart"
      >
        <template #item="{ element, index }">
          <div 
            class="form-component relative border rounded-md overflow-hidden cursor-pointer"
            :class="{ 
              'ring-2 ring-blue-500 bg-blue-50 border-blue-300 shadow-lg': selectedComponentId === element.id,
              'bg-white border-gray-200 hover:border-blue-300 hover:shadow-md hover:bg-blue-25': selectedComponentId !== element.id
            }"
            :style="{
              gridColumn: element.props.gridColumn || 'span 12'
            }"
            @click.capture="selectComponent(element)"
          >
            <!-- Selection Indicator -->
            <div 
              v-if="selectedComponentId === element.id" 
              class="absolute top-2 left-2 flex items-center space-x-1 bg-blue-500 text-white text-xs px-2 py-1 rounded-full z-[1000]"
            >
              <Icon name="heroicons:check-circle" class="w-3 h-3" />
              <span>Selected</span>
            </div>
            <div class="component-actions absolute right-1.5 top-1.5 flex space-x-1 z-10">
              <button 
                class="p-1 text-gray-400 hover:text-gray-600 rounded"
                title="Drag to reorder"
              >
                <span class="drag-handle cursor-move">
                  <Icon name="material-symbols:drag-indicator" class="w-4 h-4" />
                </span>
              </button>
              <button 
                class="p-1 text-gray-400 hover:text-blue-500 rounded"
                title="Duplicate component"
                @click.stop="duplicateComponent(element)"
              >
                <Icon name="heroicons:document-duplicate" class="w-4 h-4" />
              </button>
              <!-- <button 
                class="p-1 text-gray-400 hover:text-gray-600 rounded"
                title="Resize component"
                @click.stop="toggleResizeMode(element)"
              >
                <Icon name="material-symbols:resize" class="w-4 h-4" />
              </button> -->
              <button 
                class="p-1 text-gray-400 hover:text-red-500 rounded"
                title="Delete component"
                @click.stop="showDeleteModal(element.id)"
              >
                <Icon name="material-symbols:delete-outline" class="w-4 h-4" />
              </button>
            </div>
            
            <div class="p-3">
              <component-preview 
                :component="element" 
                :is-preview="false"
                @select-nested-component="handleNestedComponentSelect"
              />
            </div>

            <!-- Resize handle - only shown when in resize mode -->
            <div 
              v-if="resizeMode && selectedComponentId === element.id"
              class="resize-handles"
            >
              <div 
                class="resize-handle resize-handle-right"
                @mousedown.stop.prevent="startResize($event, element)"
              ></div>
            </div>
          </div>
        </template>
      </draggable>
    </div>

    <!-- Delete Confirmation Modal -->
    <RsModal v-model="showDeleteConfirmModal" title="Confirm Delete" size="md" position="center">
      <div class="p-4">
        <div class="flex items-center mb-4">
          <Icon name="material-symbols:warning-outline" class="text-yellow-500 w-8 h-8 mr-3 flex-shrink-0" />
          <div>
            <p class="text-gray-600 font-medium mb-1">Delete Component</p>
            <p class="text-gray-600 text-sm">
              Are you sure you want to delete this component? This action cannot be undone and will permanently remove the component from your form.
            </p>
          </div>
        </div>
      </div>
      <template #footer>
        <div class="flex justify-end gap-2">
          <RsButton @click="cancelDelete" variant="tertiary">
            Cancel
          </RsButton>
          <RsButton @click="confirmDelete" variant="danger">
            <Icon name="material-symbols:delete" class="w-4 h-4 mr-1" />
            Delete
          </RsButton>
        </div>
      </template>
    </RsModal>
  </div>
</template>

<script setup>
import draggable from 'vuedraggable';
import { onMounted, watch, onUnmounted, nextTick } from 'vue';

const props = defineProps({
  formComponents: {
    type: Array,
    default: () => []
  }
});

const emit = defineEmits(['select-component', 'move-component', 'delete-component', 'update-component', 'optimize-layout', 'select-nested-component', 'insert-component-at-index', 'duplicate-component']);

const selectedComponentId = ref(null);
const resizeMode = ref(false);
const resizing = ref(false);
const initialWidth = ref(0);
const initialX = ref(0);

// Delete modal state
const showDeleteConfirmModal = ref(false);
const componentToDelete = ref(null);

// Watch for changes in formComponents
watch(() => props.formComponents, (newComponents) => {
  // If the currently selected component is no longer in the list, deselect it
  if (selectedComponentId.value && 
      !newComponents.some(comp => comp.id === selectedComponentId.value)) {
    selectedComponentId.value = null;
    resizeMode.value = false;
  }
}, { deep: true });

// Create a reactive list with the form components
const componentList = computed({
  get: () => props.formComponents,
  set: (value) => {
    // Do nothing here to avoid reactivity loops - let onDragEnd handle the changes
  }
});

// Handle component selection
const selectComponent = (component) => {
  if (!component || !component.id) return;
  
  selectedComponentId.value = component.id;
  
  // Use a copy of the component to prevent reactivity issues
  // But make sure to keep the original ID
  const componentCopy = JSON.parse(JSON.stringify(component));
  componentCopy.id = component.id; // Ensure ID preservation
  
  emit('select-component', componentCopy);
};

// Handle component duplication
const duplicateComponent = (component) => {
  emit('duplicate-component', component);
};

// Show delete confirmation modal
const showDeleteModal = (id) => {
  componentToDelete.value = id;
  showDeleteConfirmModal.value = true;
};

// Cancel delete operation
const cancelDelete = () => {
  showDeleteConfirmModal.value = false;
  componentToDelete.value = null;
};

// Confirm delete operation
const confirmDelete = () => {
  if (componentToDelete.value) {
    const id = componentToDelete.value;
    if (selectedComponentId.value === id) {
      selectedComponentId.value = null;
      resizeMode.value = false;
    }
    emit('delete-component', id);
  }
  showDeleteConfirmModal.value = false;
  componentToDelete.value = null;
};

// Handle nested component selection from sections
const handleNestedComponentSelect = (component) => {
  // Pass the nested component selection up to the main form builder
  emit('select-nested-component', component);
};

// Toggle resize mode
const toggleResizeMode = (component) => {
  resizeMode.value = !resizeMode.value;
  selectedComponentId.value = component.id;
};

// Start resizing
const startResize = (event, component) => {
  resizing.value = true;
  selectedComponentId.value = component.id;
  
  // Store initial values
  initialWidth.value = parseInt(component.props.width) || 100;
  initialX.value = event.clientX;
  
  // Add event listeners
  document.addEventListener('mousemove', handleResize);
  document.addEventListener('mouseup', stopResize);
};

// Handle resize event
const handleResize = (event) => {
  if (!resizing.value || !selectedComponentId.value) return;
  
  // Calculate new width
  const component = props.formComponents.find(c => c.id === selectedComponentId.value);
  if (!component) return;
  
  // Calculate delta
  const deltaX = event.clientX - initialX.value;
  
  // Convert to percentage of container width
  const container = document.querySelector('.grid-container');
  if (!container) return;
  
  const containerWidth = container.offsetWidth;
  const deltaPercentage = (deltaX / containerWidth) * 100;
  
  // Calculate new width (with constraints)
  let newWidth = initialWidth.value + deltaPercentage;
  
  // Constrain to reasonable values
  newWidth = Math.max(25, Math.min(100, newWidth)); // Min 25%, max 100%
  
  // Get the current column span
  const currentSpanMatch = component.props.gridColumn?.match(/span\s+(\d+)/) || [];
  const currentSpan = parseInt(currentSpanMatch[1]) || 12;
  
  // Define standard widths for snap points (25%, 33%, 50%, 66%, 75%, 100%)
  const standardWidths = [25, 33, 50, 66, 75, 100];
  
  // Snap to nearest standard width if within 5%
  for (const std of standardWidths) {
    if (Math.abs(newWidth - std) < 5) {
      newWidth = std;
      break;
    }
  }
  
  // Convert precise percentages to exact grid column spans
  // This ensures the visual appearance matches the percentage
  let gridColumns;
  switch (newWidth) {
    case 25: gridColumns = 3; break;  // 3/12 = 25%
    case 33: gridColumns = 4; break;  // 4/12 = 33.33%
    case 50: gridColumns = 6; break;  // 6/12 = 50%
    case 66: gridColumns = 8; break;  // 8/12 = 66.67%
    case 75: gridColumns = 9; break;  // 9/12 = 75%
    case 100: gridColumns = 12; break; // 12/12 = 100%
    default: gridColumns = Math.round((newWidth / 100) * 12);
  }
  
  // Only update if the span actually changed to avoid unnecessary rerenders
  if (gridColumns !== currentSpan) {
    // Update component's width and grid column span
    const updatedComponent = {
      ...component,
      props: {
        ...component.props,
        width: `${newWidth}%`,
        gridColumn: `span ${gridColumns}`
      }
    };
    
    // Signal component update
    emit('update-component', updatedComponent);
    
    // Signal that a resize has occurred that might require layout optimization
    // Using nextTick to ensure the update is processed first
    nextTick(() => {
      emit('optimize-layout');
    });
  }
};

// Stop resizing
const stopResize = () => {
  resizing.value = false;
  
  // Remove event listeners
  document.removeEventListener('mousemove', handleResize);
  document.removeEventListener('mouseup', stopResize);
};

// Handle drag start from internal reordering
const onDragStart = (event) => {
  // Internal reordering
};

// Handle drag end event for reordering
const onDragEnd = (event) => {
  if (event.oldIndex !== event.newIndex) {
    emit('move-component', { 
      oldIndex: event.oldIndex, 
      newIndex: event.newIndex 
    });
  }
};

// Clean up event listeners when component is unmounted
onUnmounted(() => {
  document.removeEventListener('mousemove', handleResize);
  document.removeEventListener('mouseup', stopResize);
});
</script>

<style scoped>
.grid-container {
  display: grid;
  grid-template-columns: repeat(12, 1fr);
  grid-auto-flow: row; /* Changed: Remove 'dense' to preserve intentional spacing */
  column-gap: 16px;
  row-gap: 12px;
  width: 100%;
  padding: 0;
  box-sizing: border-box;
}

.draggable-container {
  display: contents; /* This makes draggable container not interfere with the grid */
}

.ghost {
  opacity: 0.3;
  background: #dbeafe;
  border: 2px dashed #3b82f6;
  width: 100% !important;
  grid-column: span 12 !important;
  transform: scale(0.95);
}

.form-component {
  /* Removed transition for immediate snapping */
  grid-column: span 12; /* Default to full width */
  width: 100% !important; /* Force the width within the grid cell */
  margin-bottom: 16px;
}



.form-component:hover .component-actions {
  opacity: 1;
}

.component-actions {
  opacity: 0;
  transition: opacity 0.15s ease;
}

.form-component:hover {
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
}

.resize-handles {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  pointer-events: none;
}

.resize-handle {
  position: absolute;
  pointer-events: auto;
  cursor: col-resize;
}

.resize-handle-right {
  top: 0;
  right: 0;
  width: 8px;
  height: 100%;
  background-color: rgba(37, 99, 235, 0.2);
  position: absolute;
  cursor: col-resize;
}

.resize-handle-right:hover,
.resize-handle-right:active {
  background-color: rgba(37, 99, 235, 0.4);
  width: 8px;
}
</style> 