import { NButton } from 'naive-ui';
import {
  defineComponent,
  nextTick,
  onMounted,
  reactive,
  ref,
  watch
} from 'vue';
import styles from './index.module.less';
import { getImage } from './images';
import {px2vw,px2vwH} from '@/utils/index'
export default defineComponent({
  name: 'coai-guide',
  emits: ['close'],
  setup(props, { emit }) {
    const data = reactive({
      box: {
        height:'0px',
      } as any,
      show: false,
      /**
       *
            width:  px2vw(840),
            height:  px2vw(295)
       */
      steps: [
        {
          ele: '',
          eleRect: {} as DOMRect,
          img: getImage('class1.png'),
          handStyle: {
            top: '0.91rem'
          },
          imgStyle: {
            left: '-294px',
            width:  '648px',
            height:  '227px'
          },
          btnsStyle: {
            bottom:'30px',
            left: '-90px',
          },
          eleRectPadding:{
            left:7,
            top:7,
            width:14,
            height:14
          }
        },
        {
          ele: '',
          img: getImage('class2.png'),
          imgStyle: {
            top: '100%',
            left:  '-283px',
            width: '515px',
            height:'227px'
          },
          btnsStyle: {
            bottom: '30px',
            left: px2vw(-90),
          },
          boxStyle:{
            borderRadius:'25px'
          },
          eleRectPadding:{
            left:7,
            top:7,
            width:14,
            height:14
          }

        },
        {
          ele: '',
          img: getImage('class3.png'),
          imgStyle: {
            width:  '437px',
            height:  '227px',
            left:'-282px'
          },
          btnsStyle: {
            bottom: '30px',
            left: '-130px',
          },
          eleRectPadding:{
            left:7,
            top:7,
            width:14,
            height:14
          }
        },

      ],
      step: 0
    });
    const tipShow = ref(false);
    const guideInfo = localStorage.getItem('teacher-guideInfo');
    if (guideInfo && JSON.parse(guideInfo).classGuide) {
      tipShow.value = false;
    } else {
      tipShow.value = true;
    }
    const getStepELe = () => {

      const ele: HTMLElement = document.getElementById(`class-${data.step}`)!;

      if (ele) {
        const eleRect = ele.getBoundingClientRect();

        const left = data.steps[data.step].eleRectPadding?.left || 0;
        const top = data.steps[data.step].eleRectPadding?.top || 0;
        const width = data.steps[data.step].eleRectPadding?.width || 0;
        const height = data.steps[data.step].eleRectPadding?.height || 0
        data.box = {
          left: eleRect.x - left+ 'px',
          top: eleRect.y - top +'px',
          width: eleRect.width + width+'px',
          height: eleRect.height +height+ 'px'
        };
        console.log(`coai-${data.step}`,data.box);
      }
    };
    onMounted(() => {
      getStepELe();
      window.addEventListener("resize", resetSize);
    });
    const resetSize = ()=>{
      getStepELe();
    }

    onUnmounted(()=>{
      window.removeEventListener("resize", resetSize);
    })

    const handleNext = () => {
      if (data.step >= 4) {
        endGuide();
        return;
      }
      data.step = data.step + 1;
      getStepELe();
    };

    const endGuide = () => {
      let guideInfo =
        JSON.parse(localStorage.getItem('teacher-guideInfo')||'{}') || null;
      if (!guideInfo) {
        guideInfo = { classGuide: true };
      } else {
        guideInfo.classGuide = true;
      }
      localStorage.setItem('teacher-guideInfo', JSON.stringify(guideInfo));
      tipShow.value = false;
      //  localStorage.setItem('endC')
    };
    return () => (
      <>
        {tipShow.value ? (
          <div
            v-model:show={tipShow.value}
            class={['n-modal-mask', 'n-modal-mask-guide']}>
            <div class={styles.content} onClick={() => handleNext()}>
              <div
                class={styles.backBtn}
                onClick={(e: Event) => {
                  e.stopPropagation();
                  endGuide();
                }}>
                跳过
              </div>
              <div
                class={styles.box}
                style={{...data.box,...data.steps[data.step].boxStyle}}
                id={`modeType-${data.step}`}>

                {data.steps.map((item: any, index) => (

                  <div
                    onClick={(e: Event) => e.stopPropagation()}
                    class={styles.item}
                    style={ item.type=='bottom'?  {
                      display: index === data.step ? '' : 'none',
                      left: `${item.eleRect?.left}px`,
                      top:`-${item.imgStyle?.height}`

                    }:{
                      display: index === data.step ? '' : 'none',
                      left: `${item.eleRect?.left}px`,
                     top: `${data.box?.height}`,

                    }}>
                    <img
                      class={styles.img}
                      style={item.imgStyle}
                      src={item.img}
                    />
                    {/* <img
                      class={styles.iconHead}
                      style={item.handStyle}
                      src={getImage('indexDot.png')}
                    /> */}
                    <div class={styles.btns} style={item.btnsStyle}>
                      {data.step + 1 == data.steps.length ? (
                        <>
                         <div
                            class={[styles.endBtn]}
                            onClick={() => endGuide()}>
                            完成
                          </div>
                          <div
                            class={styles.nextBtn}
                            onClick={() => {
                              data.step = 0;
                              getStepELe();
                            }}>
                            再看一遍
                          </div>

                        </>
                      ) : (
                        <div class={styles.btn} onClick={() => handleNext()}>
                          下一步 ({data.step + 1}/{data.steps.length})
                        </div>
                      )}
                    </div>
                  </div>
                ))}
              </div>
            </div>
          </div>
        ) : null}
      </>
    );
  }
});
