<template>
  <div class="tool-list">
    <h3>请在下方选择正确的工具（{{ selectedToolNum }}/{{ requiredToolList.length }}）</h3>
    <div class="wrapper">
      <ul
        ref="toolListEl"
        @scroll="onToolListElScroll"
      >
        <!-- @touchmove.prevent -->
        <li
          v-for="(toolItem, idx) in toolList"
          :key="toolItem.name"
          @click="onClickToolItem(idx)"
        >
          <div
            class="tool-frame"
            :class="{
              active: toolItem.selected,
            }"
          >
            <img
              class=""
              :class="{active: toolItem.selected,
              }"
              :src="toolItem.url"
              alt=""
              draggable="false"
            >
          </div>
          <span
            class="tool-name"
            :class="{
              active: toolItem.selected,
            }"
          >{{ toolItem.name }}</span>
        </li>
      </ul>
      <button
        v-show="isShowLeftArrow"
        class="arrow-left"
        @click="onClickLeftArrow"
      >
        <img
          class=""
          src="@/assets/images/arrow-left.png"
          alt=""
          draggable="false"
        >
      </button>
      <button
        v-show="isShowRightArrow"
        class="arrow-right"
        @click="onClickRightArrow"
      >
        <img
          class=""
          src="@/assets/images/arrow-right.png"
          alt=""
          draggable="false"
        >
      </button>
    </div>
  </div>
</template>

<script setup>
import { ref, watch, computed } from "vue"
import { toolList, selectedToolNum, showToast } from "@/store/index.js"

const {
  windowSizeInCssForRef,
  windowSizeWhenDesignForRef,
} = useSizeAdapt()

const props = defineProps(['currentStepInfo'])

const requiredToolList = computed(() => {
  return props.currentStepInfo.toolIdxList
})

/**
 * 选中工具逻辑
 */
function onClickToolItem(idx) {
  if (requiredToolList.value.includes(idx)) {
    if (toolList.value[idx].selected === false) {
      if (requiredToolList.value.length === 1) {
        toolList.value[idx].selected = true
        showToast('选择正确')
      } else if (!props.currentStepInfo.hasToolOrder) {
        toolList.value[idx].selected = true
        showToast('选择正确')
      } else {
        const toolIdxInRequiredToolList = requiredToolList.value.indexOf(idx)
        if (toolIdxInRequiredToolList === 0) {
          toolList.value[idx].selected = true
          showToast('选择正确')
        } else {
          for (let i = 0; i < toolIdxInRequiredToolList; i++) {
            const preToolIdxInToolList = requiredToolList.value[i]
            if (!toolList.value[preToolIdxInToolList].selected) {
              showToast('您拿错工具了~请重新选择')
              return
            }
            toolList.value[idx].selected = true
            showToast('选择正确')
          }
        }
      }
    }
  } else {
    showToast('您拿错工具了~请重新选择')
  }
}

/**
 * 左右scroll逻辑
 */
const toolListEl = ref(null)
const isShowLeftArrow = ref(false)
const isShowRightArrow = ref(true)
function onToolListElScroll() {
  if (toolListEl.value.scrollLeft === 0) {
    isShowLeftArrow.value = false
  } else {
    isShowLeftArrow.value = true
  }
  if (toolListEl.value.scrollWidth - toolListEl.value.scrollLeft - toolListEl.value.clientWidth === 0) {
    isShowRightArrow.value = false
  } else {
    isShowRightArrow.value = true
  }
}
function onClickLeftArrow() {
  toolListEl.value.scrollLeft -= toolListEl.value.scrollWidth / 8
}
function onClickRightArrow() {
  toolListEl.value.scrollLeft += toolListEl.value.scrollWidth / 8
}
</script>

<style lang="less" scoped>
.tool-list{
  >h3{
    text-align: center;
    font-size: calc(14 / v-bind('windowSizeWhenDesignForRef') * v-bind('windowSizeInCssForRef'));
    font-family: Source Han Sans CN-Regular, Source Han Sans CN;
    font-weight: 400;
    color: #FFFFFF;
    line-height: calc(16 / v-bind('windowSizeWhenDesignForRef') * v-bind('windowSizeInCssForRef'));
    margin-bottom: calc(9 / v-bind('windowSizeWhenDesignForRef') * v-bind('windowSizeInCssForRef'));
  }
  >.wrapper{
    position: relative;
    >ul{
      display: flex;
      overflow: auto;
      padding-top: calc(19 / v-bind('windowSizeWhenDesignForRef') * v-bind('windowSizeInCssForRef'));
      &::-webkit-scrollbar { height: 0; };
      >li{
        flex: 0 0 auto;
        display: inline-block;
        text-align: center;
        margin-right: calc(5 / v-bind('windowSizeWhenDesignForRef') * v-bind('windowSizeInCssForRef'));;
        >.tool-frame{
          width: calc(83 / v-bind('windowSizeWhenDesignForRef') * v-bind('windowSizeInCssForRef'));
          height: calc(83 / v-bind('windowSizeWhenDesignForRef') * v-bind('windowSizeInCssForRef'));
          background: rgba(255,255,255,0.3);
          border-radius: 2px 2px 2px 2px;
          border: 1px solid #FFFFFF;
          margin-bottom: calc(3 / v-bind('windowSizeWhenDesignForRef') * v-bind('windowSizeInCssForRef'));;
          position: relative;
          >img{
            position: absolute;
            left: 0;
            bottom: 0;
            width: 100%;
            height: 100%;
          }
          >img.active{
            width: 125%;
            height: 125%;
          }
        }
        >.tool-frame.active{
          border: calc(4 / v-bind('windowSizeWhenDesignForRef') * v-bind('windowSizeInCssForRef')) solid #FFE096;
        }
        >span{
          font-size: calc(14 / v-bind('windowSizeWhenDesignForRef') * v-bind('windowSizeInCssForRef'));
          font-family: Source Han Sans CN-Regular, Source Han Sans CN;
          font-weight: 400;
          color: #FFFFFF;
          line-height: calc(16 / v-bind('windowSizeWhenDesignForRef') * v-bind('windowSizeInCssForRef'));
        }
        >span.active{
          font-size: calc(14 / v-bind('windowSizeWhenDesignForRef') * v-bind('windowSizeInCssForRef'));
          font-family: Source Han Sans CN-Bold, Source Han Sans CN;
          font-weight: bold;
          color: #FFE096;
          line-height: calc(16 / v-bind('windowSizeWhenDesignForRef') * v-bind('windowSizeInCssForRef'));
        }
      }
      >li:first-of-type{
        margin-left: calc(12 / v-bind('windowSizeWhenDesignForRef') * v-bind('windowSizeInCssForRef'));;
      }
      >li:last-of-type{
        margin-right: calc(12 / v-bind('windowSizeWhenDesignForRef') * v-bind('windowSizeInCssForRef'));;
      }
    }
    >button.arrow-left{
      position: absolute;
      left: calc(8 / v-bind('windowSizeWhenDesignForRef') * v-bind('windowSizeInCssForRef'));;
      top: 50%;
      transform: translateY(-50%);
      height: calc(24 / v-bind('windowSizeWhenDesignForRef') * v-bind('windowSizeInCssForRef'));;
      >img{
        height: 100%;
      }
    }
    >button.arrow-right{
      position: absolute;
      right: calc(8 / v-bind('windowSizeWhenDesignForRef') * v-bind('windowSizeInCssForRef'));
      top: 50%;
      transform: translateY(-50%);
      height: calc(24 / v-bind('windowSizeWhenDesignForRef') * v-bind('windowSizeInCssForRef'));;
      >img{
        height: 100%;
      }
    }
  }
}
</style>