import { IconRotate , IconMove} from "@/assets/icons";
import { CompToolbars } from "@/modules/editor/objects/Toolbars";
import { css } from "@linaria/core";
import { defineComponent, onMounted, onUnmounted, ref, nextTick, reactive, watch } from "vue";
import { useEditor } from "../../../..";


export const SelectTransfer = defineComponent({
  setup() {
    const editor = useEditor();
    const { controls, store } = editor;
    const { selectCtrl } = controls;
    const { transferStyle } = selectCtrl;
    const toolbarRef = ref<HTMLElement>();
  
    const state = reactive({
       toolbarW: 0,
       toolbarOpts: [] as any[],
    })

    watch(()=>[store.selectId, store.selected], ()=>{
      console.log("changing--")
      if (store.selected.length == 1) {
        const comp = store.compMap[store.selected[0]];
        //@ts-ignore
        state.toolbarOpts = CompToolbars[comp.compKey] || CompToolbars.default;
      }  else {
        state.toolbarOpts = CompToolbars.MultiSelector;
      }
      nextTick(()=>{
        nextTick(()=>{
          if (!toolbarRef.value) {
            return
          }  
          const r = toolbarRef.value.getBoundingClientRect();
          state.toolbarW = r.width;
        })
      })
    })

  
    return () => {
      
      let comp: any = null;
      if (store.selected.length == 1) {
        comp = store.compMap[store.selected[0]];
      }

      const w :any = selectCtrl.objContainer?.getBound();
      const isTextEdit = selectCtrl.selected.length == 1 && selectCtrl.selected[0].comp.compKey == "Text";
  
      return (
        <div
          class={[
            "absolute transfer z-998",
            transferStyle.showGizmo && transferStyle.mode != 2 ? showgizmo : hideGizmo,
          ]}
          style={{
            top: transferStyle.baseCardTop
          }}
        >
          <div
            id= "toolbar"
            class={toolbarStyle}
            style={{
              top: w?.y + "px",
              left: (w?.x + w?.width / 2.0 - state.toolbarW / 2.0)  + "px",
            }}
            ref= {toolbarRef}
          >
            {
              state.toolbarOpts.map((item) => {
                return item.getVisible.call(editor, comp) ? (
                  <item.component
                    class="p-4px"
                    value={item.getValue?.(comp)}
                    onClick={() => item.onClick.call(editor, comp)}
                  />
                ) : null;
              })
            }
          </div>

          <div
            class={["absolute", selctRectStyle]}
            id="movecenter"
            style={{
              width: transferStyle.width + "px",
              height: transferStyle.height + "px",
              transform: transferStyle.matrix,
              transformOrigin: `0 0`,
            }}
          >
            <div class={borderStyle} style={{ transform: transferStyle.matrixInvert, transformOrigin: `0 0`}} >
                <div class={borderContentStyle} style={{width: transferStyle.relWidth + "px", height: transferStyle.relHeight + "px"}}></div>
            </div>
            <>
              
              {
                !isTextEdit &&  <div
                  class={[resizeStyle, scaleBottomRightStyle]}
                  style={{ transform: transferStyle.matrixInvert }}
                  id="scaleBottomright"
                />
              }

                {
                  !isTextEdit &&   <div
                    class={[resizeStyle, scaleBottomLeftStyle]}
                    style={{ transform: transferStyle.matrixInvert }}
                    id="scaleBottomleft"
                  />
                }

              {
                  !isTextEdit &&<div
                  class={[resizeStyle, scaleTopLeftStyle]}
                  style={{ transform: transferStyle.matrixInvert }}
                  id="scaleTopleft"
                />
              }
              {
                  !isTextEdit && <div
                  class={[resizeStyle, scaleTopRightStyle]}
                  style={{ transform: transferStyle.matrixInvert }}
                  id="scaleTopright"
                />
              }

              <div class={transformBtnsStyle} style={{ transform: transferStyle.matrixInvert }}>

                <div
                    class={transBtnStyle}
                    id="moveicon"
                  >
                    <IconMove />
                </div>

                <div
                  class={transBtnStyle}
                  id = "rotate"
                >
                  <IconRotate  />
                </div>
              </div>

              {transferStyle.showOrthScale && !isTextEdit && (
                <div
                  class={[resizeHeightBtnCls, scaleTopCls]}
                  style={{ transform: transferStyle.matrixInvert }}
                  id="scaletop"
                />
              )}
              {transferStyle.showOrthScale && !isTextEdit && (
                <div
                  class={[resizeHeightBtnCls, scaleBottomCls]}
                  style={{ transform: transferStyle.matrixInvert }}
                  id="scalebottom"
                />
              )}
              {transferStyle.showOrthScale &&  (
                <div
                  class={[resizeWidthBtnCls, scaleRightCls]}
                  style={{ transform: transferStyle.matrixInvert }}
                  id="scaleright"
                />
              )}
              {transferStyle.showOrthScale && (
                <div
                  class={[resizeWidthBtnCls, scaleLeftCls]}
                  style={{ transform: transferStyle.matrixInvert }}
                  id="scaleleft"
                />
              )}
            </>
          </div>
        </div>
      );
    };
  },
});
const selctRectStyle = css`
  pointer-events: none;
`;
const showgizmo = css`
  display: block;
  left: 0;
  top: 0;
  /* pointer-events: none; */
`;
const hideGizmo = css`
  display: none;
`;

const borderStyle = css`
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 999;
`;
const borderContentStyle = css`
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  outline: 2px solid @inf-primary-color;
`

const resizeStyle = css`
  position: absolute;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background-color: #fff;
  z-index: 999;
  transform: translate(50%, 50%);
  pointer-events: auto;
  box-shadow: 0 0 2px 2px rgba(0, 0, 0, 0.2);
  cursor: nwse-resize;
  &:hover {
    border-color: @inf-primary-color;
  }
`;

const scaleBottomRightStyle = css`
  bottom: -8px;
  right: -8px;
`;
const scaleBottomLeftStyle = css`
  bottom: -8px;
  left: -8px;
`;

const scaleTopLeftStyle = css`
  top: -8px;
  left: -8px;
`;

const scaleTopRightStyle = css`
  top: -8px;
  right: -8px;
`;

const transformBtnsStyle = css`
  @apply space-x-10px whitespace-nowrap;
  position: absolute;
  bottom: 0px;
  left:calc(50% - 32px);
  font-size: 16px;
  z-index: 999;
  pointer-events: auto;
  transform-origin: 50% 100%;
  pointer-events: none;
`;

const transBtnStyle = css`
  display: inline-block;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background-color: #fff;
  text-align: center;
  line-height: 28px;
  font-size: 16px;
  color: #333;
  position: relative;
  top: 50px;
  @apply shadow cursor-move;
  pointer-events: auto;

  &:hover {
    color: #fff;
    background-color: @inf-primary-color;
  }
`;

const toolbarStyle = css`
  @apply bg-white shadow rounded space-x-4px p-4px whitespace-nowrap;
  position: absolute;
  top: 0;
  left: 50%;
  transform: translate(0%, -60px);
  z-index: 999;
`;

const resizeHeightBtnCls = css`
  position: absolute;
  width: 30px;
  height: 8px;
  border-radius: 4px;
  left: 50%;
  transform: translate(-50%, -4px);
  pointer-events: auto;
  cursor: ns-resize;
  background: rgba(255, 255, 255, 0.3);
  &:hover {
    background: @inf-primary-color;
  }
  @apply shadow;
  z-index: 999;
`;

const scaleTopCls = css`
  top: -4px;
  left: calc(50% - 15px);
`;

const scaleBottomCls = css`
  bottom:-4px;
  left: calc(50% - 15px);
`;

const resizeWidthBtnCls = css`
  position: absolute;
  width: 8px;
  height: 30px;
  border-radius: 4px;
  pointer-events: auto;
  cursor: ew-resize;
  background: rgba(255, 255, 255, 0.3);
  &:hover {
    background: @inf-primary-color;
  }
  @apply shadow;
  z-index: 999;
`;

const scaleRightCls = css`
  right: -4px;
  top: calc(50% - 15px);
`;

const scaleLeftCls = css`
  left: -4px;
  top: calc(50% - 15px);
`;
