import { css, cx } from "@linaria/core";
import { Button, Tooltip } from "ant-design-vue";
import { computed, reactive } from "vue";

import {
  IconAi,
  IconApplication,
  IconCloud,
  IconImage,
  IconText,
  IconTpl,
  IconVideo,
} from "@/assets/icons";
import { IconCube, IconLayers } from "@queenjs/icons";
import { defineUI } from "queenjs";
import { CompTree } from "../SliderRight/CompTree";
import AiText from "./AiText";
import Application from "./Application";
import CustomComps from "./CustomComps";
import { Sources } from "./Sources";
import Templates from "./Templates";
import Text from "./Text";

const tabs = [
  {
    title: "模板",
    icon: IconTpl,
    component: Templates,
  },
  {
    title: "组合",
    icon: IconCube,
    content: [
      {
        title: "平台",
        component: CustomComps,
        props: { compType: "senior" },
      },
      {
        title: "我的",
        component: CustomComps,
        props: { compType: "user" },
      },
    ],
  },
  {
    title: "文字",
    icon: IconText,
    component: Text,
  },
  {
    title: "图片",
    icon: IconImage,
    component: Sources,
    props: { sourceType: "Image", sourceFrom: "system" },
  },
  // {
  //   title: "形状",
  //   icon: IconShape,
  //   component: Shapes,
  // },
  {
    title: "视频",
    icon: IconVideo,
    component: Sources,
    props: { sourceType: "Video", sourceFrom: "system" },
  },
  {
    title: "组件",
    icon: IconApplication,
    component: Application,
  },
  {
    title: "已上传",
    icon: IconCloud,
    content: [
      {
        title: "图片",
        component: Sources,
        props: { sourceType: "Image", sourceFrom: "user" },
      },
      {
        title: "视频",
        component: Sources,
        props: { sourceType: "Video", sourceFrom: "user" },
      },
    ],
  },
  {
    title: "AI",
    icon: IconAi,
    component: AiText,
  },
  {
    title: "图层",
    icon: () => <IconLayers class="text-24px transform scale-170" />,
    component: CompTree,
  },
];

export default defineUI({
  setup() {
    // @ts-ignore
    const state = reactive({
      tabIndex: 0,
      compIndexs: [0, 0, 0, 0, 0, 0, 0],
      currentTab: computed(() => {
        return tabs[state.tabIndex];
      }),
      currCompIndex: computed(() => {
        return state.compIndexs[state.tabIndex];
      }),
    });

    return () => {
      const { tabIndex, currentTab, currCompIndex } = state;
      const currComp = currentTab.component
        ? currentTab
        : currentTab.content?.[state.currCompIndex];

      return (
        <div class="h-full flex">
          <div class="flex flex-col w-70px py-16px border-right !border-2px">
            {tabs.map((record, index) => {
              return (
                <>
                  {index === tabs.length - 2 && <div class="flex-1"></div>}
                  <div
                    key={index}
                    class={cx(
                      tabItem,
                      "relative mt-10px py-8px text-center cursor-pointer text-light-50 transition",
                      state.tabIndex == index && "active"
                    )}
                    onClick={() => (state.tabIndex = index)}
                  >
                    <Tooltip title={record.title} placement="right">
                      <record.icon class="text-28px p-10px" />
                    </Tooltip>
                  </div>
                </>
              );
            })}
          </div>
          <div class="flex-1 h-1/1 overflow-hidden flex flex-col">
            {currentTab.content && (
              <div class="mt-5px ml-10px space-x-10px">
                {currentTab.content?.map((item: any, index: number) => (
                  <Button
                    key={index}
                    type="text"
                    class={cx(
                      "transition",
                      currCompIndex == index && "font-bold"
                    )}
                    onClick={() => (state.compIndexs[tabIndex] = index)}
                  >
                    {item?.title}
                  </Button>
                ))}
              </div>
            )}

            <currComp.component
              class="flex-1 h-1/1 px-16px !my-10px overflow-y-auto"
              {...currComp.props}
            />
          </div>
        </div>
      );
    };
  },
});

const tabItem = css`
  &:hover {
    color: @inf-primary-color;
  }
  &.active {
    color: @inf-primary-color;
    &:before {
      content: "";
      position: absolute;
      left: 0;
      top: 0;
      height: 100%;
      width: 3px;
      background-color: @inf-primary-color;
    }
  }
`;
