<template>
  <div class="icon-demo">
    <h1>TUIIcon 图标</h1>
    <p class="component-desc">提供了一套常用的图标集合，支持自定义尺寸和颜色。</p>

    <!-- Icon Gallery -->
    <DemoBlock
      title="图标集合"
      description="点击图标可复制图标名称。使用搜索框快速查找图标。"
      :code="basicCode"
    >
      <div class="control-panel">
        <div class="search-bar">
          <TUIInput
            v-model="searchText"
            placeholder="搜索图标"
            :prefix-icon="IconSearch"
            clearable
          />
        </div>

        <div class="property-controls">
          <div class="control-group">
            <label class="control-label">图标尺寸:</label>
            <div class="size-options">
              <button
                v-for="size in sizeOptions"
                :key="size.value"
                :class="['size-btn', { active: selectedSize === size.value }]"
                @click="selectedSize = size.value"
              >
                {{ size.label }}
              </button>
            </div>
          </div>

          <div class="control-group">
            <label class="control-label">图标颜色:</label>
            <div class="color-options">
              <button
                v-for="color in colorOptions"
                :key="color.value"
                :class="['color-btn', { active: selectedColor === color.value }]"
                :style="{ backgroundColor: color.value }"
                @click="selectedColor = color.value"
              />
              <input
                v-model="customColor"
                type="color"
                class="custom-color-input"
                @change="selectedColor = customColor"
              >
            </div>
          </div>
        </div>
      </div>

      <div class="icon-list">
        <div
          v-for="(icon, name) in filteredIcons"
          :key="name"
          class="icon-item"
          @click="copyIconName(name)"
        >
          <component
            :is="icon"
            class="icon"
            :size="selectedSize"
            :color="selectedColor"
          />
          <div class="icon-name">{{ name }}</div>
        </div>
      </div>
    </DemoBlock>

    <!-- Usage Example -->
    <DemoBlock
      title="基础用法"
      description="直接引入图标组件并使用。"
      :code="usageCode"
    >
      <div class="demo-row">
        <IconSearch :size="24" />
        <IconClose :size="24" />
        <IconCheck :size="24" color="#10b981" />
        <IconWarning :size="24" color="#f59e0b" />
        <IconError :size="24" color="#ef4444" />
      </div>
    </DemoBlock>

    <!-- API Documentation -->
    <section class="api-section">
      <h2>API</h2>

      <div class="api-table-wrapper">
        <h3>Props</h3>
        <div class="api-table">
          <div class="api-table-header">
            <div class="api-table-cell">属性名</div>
            <div class="api-table-cell">说明</div>
            <div class="api-table-cell">类型</div>
            <div class="api-table-cell">默认值</div>
          </div>
          <div class="api-table-row">
            <div class="api-table-cell">size</div>
            <div class="api-table-cell">图标尺寸</div>
            <div class="api-table-cell">string | number</div>
            <div class="api-table-cell">-</div>
          </div>
          <div class="api-table-row">
            <div class="api-table-cell">color</div>
            <div class="api-table-cell">图标颜色</div>
            <div class="api-table-cell">string</div>
            <div class="api-table-cell">-</div>
          </div>
        </div>
      </div>

      <div class="api-table-wrapper">
        <h3>Events</h3>
        <div class="api-table">
          <div class="api-table-header">
            <div class="api-table-cell">事件名</div>
            <div class="api-table-cell">说明</div>
            <div class="api-table-cell">回调参数</div>
          </div>
          <div class="api-table-row">
            <div class="api-table-cell">click</div>
            <div class="api-table-cell">点击图标时触发</div>
            <div class="api-table-cell">(event: Event)</div>
          </div>
        </div>
      </div>
    </section>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue';
import { TUIInput } from '../src/components';
import * as Icons from '../src/components/Icon';
import { IconSearch, IconClose, IconCheck, IconWarning, IconError } from '../src/components/Icon';
import DemoBlock from './components/DemoBlock.vue';

const searchText = ref('');
const selectedSize = ref<string | number>(24);
const selectedColor = ref<string>('#333333');
const customColor = ref<string>('#333333');

const sizeOptions = [
  { label: '16px', value: 16 },
  { label: '20px', value: 20 },
  { label: '24px', value: 24 },
  { label: '32px', value: 32 },
  { label: '48px', value: 48 },
];

const colorOptions = [
  { label: '默认', value: '#333333' },
  { label: '红色', value: '#ef4444' },
  { label: '蓝色', value: '#3b82f6' },
  { label: '绿色', value: '#10b981' },
  { label: '橙色', value: '#f59e0b' },
  { label: '紫色', value: '#8b5cf6' },
];

const iconComponents = computed(() => {
  const icons: Record<string, any> = {};
  Object.entries(Icons).forEach(([name, component]) => {
    if (name.startsWith('Icon')) {
      icons[name] = component;
    }
  });
  return icons;
});

const filteredIcons = computed(() => {
  const text = searchText.value.toLowerCase();
  if (!text) {
    return iconComponents.value;
  }
  const filtered: Record<string, any> = {};
  Object.entries(iconComponents.value).forEach(([name, component]) => {
    if (name.toLowerCase().includes(text)) {
      filtered[name] = component;
    }
  });
  return filtered;
});

const copyIconName = async (name: string) => {
  try {
    await navigator.clipboard.writeText(name);
    console.log(`Copied: ${name}`);
  } catch (err) {
    console.error('Failed to copy:', err);
  }
};

const basicCode = `<template>
  <div class="icon-list">
    <div v-for="(icon, name) in icons" :key="name" class="icon-item">
      <component :is="icon" :size="24" :color="color" />
      <span>{{ name }}</span>
    </div>
  </div>
</template>

<script setup>
import * as Icons from '@/components/Icon';
<\/script>`;

const usageCode = `<template>
  <IconSearch :size="24" />
  <IconClose :size="24" />
  <IconCheck :size="24" color="#10b981" />
  <IconWarning :size="24" color="#f59e0b" />
  <IconError :size="24" color="#ef4444" />
</template>

<script setup>
import { IconSearch, IconClose, IconCheck, IconWarning, IconError } from '@/components/Icon';
<\/script>`;
</script>

<style lang="scss" scoped>
@import './styles/demo-common.scss';

.control-panel {
  width: 100%;
  margin-bottom: 24px;
  padding: 20px;
  background: var(--bg-color-secondary);
  border-radius: 8px;
  box-sizing: border-box;
}

.search-bar {
  margin-bottom: 20px;
  max-width: 300px;
}

.property-controls {
  display: flex;
  gap: 32px;
  flex-wrap: wrap;
}

.control-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.control-label {
  font-size: 14px;
  font-weight: 500;
  color: var(--text-color-secondary);
}

.size-options {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.size-btn {
  padding: 6px 12px;
  border: 1px solid var(--stroke-color-secondary);
  border-radius: 4px;
  background: var(--bg-color-primary);
  cursor: pointer;
  font-size: 12px;
  transition: all 0.2s;
  color: var(--text-color-primary);

  &:hover {
    border-color: var(--text-color-link);
  }

  &.active {
    background: var(--text-color-link);
    color: #fff;
    border-color: var(--text-color-link);
  }
}

.color-options {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}

.color-btn {
  width: 24px;
  height: 24px;
  border: 2px solid #fff;
  border-radius: 4px;
  cursor: pointer;
  box-shadow: 0 1px 3px var(--shadow-color);
  transition: all 0.2s;

  &:hover {
    transform: scale(1.1);
  }

  &.active {
    border-color: var(--text-color-link);
    transform: scale(1.1);
  }
}

.custom-color-input {
  width: 24px;
  height: 24px;
  border: 2px solid #fff;
  border-radius: 4px;
  cursor: pointer;
  background: transparent;
  box-shadow: 0 1px 3px var(--shadow-color);

  &::-webkit-color-swatch-wrapper {
    padding: 0;
  }

  &::-webkit-color-swatch {
    border: none;
    border-radius: 2px;
  }
}

.icon-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 16px;
  width: 100%;
}

.icon-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 16px 12px;
  background: var(--bg-color-primary);
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s;
  border: 1px solid var(--stroke-color-secondary);

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 16px var(--shadow-color);
    border-color: var(--text-color-link);
  }

  .icon {
    margin-bottom: 8px;
  }

  .icon-name {
    font-size: 11px;
    color: var(--text-color-tertiary);
    text-align: center;
    word-break: break-all;
    line-height: 1.4;
  }
}
</style>
