<template>
  <div class="calculator-container">
    <v-row justify="center">
      <!-- 参数输入区域 -->
      <v-col
        cols="12"
        lg="5"
        md="6"
      >
        <v-card
          class="pa-6 parameter-card"
          elevation="8"
          rounded="xl"
        >
          <v-card-title class="text-h5 mb-6 d-flex align-center">
            <v-icon
              class="mr-3"
              color="primary"
              icon="mdi-tune"
              size="large"
            />
            {{ $t('parameters') }}
          </v-card-title>

          <v-row>
            <v-col cols="6">
              <v-select
                v-model="currentSelect"
                density="comfortable"
                :items="selects"
                :label="`${$t('machineModel')}`"
                @update:model-value="updateParams"
              />
            </v-col>
            <v-col cols="12">
              <param-input-field
                v-model="maxWheelbase"
                :disabled="!isCustomMode"
                :label="`${$t('maxWheelbase')} (${maxWheelbase.unit})`"
              />
            </v-col>
            <v-col cols="12">
              <param-input-field
                v-model="hubDiameter"
                :disabled="!isCustomMode"
                :label="`${$t('hubDiameter')} (${hubDiameter.unit})`"
              />
            </v-col>
            <v-col cols="12">
              <param-input-field
                v-model="paperCoreDiameter"
                :label="`${$t('paperCoreDiameter')} (${paperCoreDiameter.unit})`"
              />
            </v-col>
            <v-col cols="12">
              <param-input-field
                v-model="paperRollWallThickness"
                :label="`${$t('paperRollWallThickness')} (${paperRollWallThickness.unit})`"
              />
            </v-col>
          </v-row>
        </v-card>
      </v-col>

      <!-- 结果显示区域 -->
      <v-col
        cols="12"
        lg="5"
        md="6"
      >
        <v-card
          class="pa-6 result-card"
          elevation="8"
          rounded="xl"
        >
          <v-card-title class="text-h5 mb-6 d-flex align-center">
            <v-icon
              class="mr-3"
              color="primary"
              icon="mdi-calculator"
              size="large"
            />
            {{ $t('results') }}
          </v-card-title>

          <v-row>
            <v-col cols="12">
              <result-card
                :label="`${$t('recommendBeltThickness')}`"
                :value="result.recommendBeltThickness"
              />
            </v-col>
            <v-col cols="12">
              <result-card
                :label="`${$t('recommendBeltWidth')}`"
                :value="result.recommendBeltWidth"
              />
            </v-col>
            <v-col cols="12">
              <result-card
                :label="`${$t('recommendBeltLength')}`"
                :value="result.recommendBeltLength"
              />
            </v-col>
          </v-row>
        </v-card>
      </v-col>
    </v-row>

  </div>
</template>

<script setup lang="ts">
  import { computed, ref } from 'vue'
  import { useI18n } from 'vue-i18n'
  import { createParam, type Param } from '@/types/param'

  const { t, locale } = useI18n()

  const currentSelect = ref('')
  const currentSelectIndex = ref(0)

  const selects = computed(() => [
    t('50_120Series'),
    t('200_Series'),
    t('600_Series'),
    t('PT23-120_Series'),
    t('PT23-200_Series'),
    t('custom'),
  ])

  const maxWheelbase = ref<Param>(createParam(900, 'mm'))
  const hubDiameter = ref<Param>(createParam(215, 'mm'))
  const paperCoreDiameter = ref<Param>(createParam(76.2, 'mm'))
  const paperRollWallThickness = ref<Param>(createParam(10, 'mm'))

  const result = computed(() => {
    const hubCircumference = Math.PI * hubDiameter.value.value
    const paperRollExternalDiameter = paperCoreDiameter.value.value + paperRollWallThickness.value.value
    const paperRollCircumference = Math.PI * paperRollExternalDiameter

    const recommendBeltLength = Math.round((paperRollCircumference + hubCircumference + (maxWheelbase.value.value - 50) * 2) / 10) * 10

    const beltWidthMapping = [
      { threshold: 20, width: 10 },
      { threshold: 25, width: 15 },
      { threshold: 32, width: 20 },
      { threshold: 40, width: 25 },
      { threshold: 70, width: 30 },
      { threshold: 95, width: 40 },
      { threshold: 125, width: 50 },
      { threshold: 150, width: 60 },
      { threshold: 204, width: 80 },
      { threshold: 380, width: 105 },
      { threshold: 950, width: 120 },
      { threshold: 1200, width: 150 },
      { threshold: 5000, width: 200 },
    ]

    const beltThicknessMapping = [
      { threshold: 10, thickness: 6 },
      { threshold: 15, thickness: 8 },
      { threshold: 20, thickness: 10 },
      { threshold: 30, thickness: 12 },
    ]

    function getBeltWidth (circumference: number): number {
      const mapping = beltWidthMapping.find(item => circumference < item.threshold)
      return mapping ? mapping.width : 0
    }

    function getBeltThickness (width: number): number {
      const mapping = beltThicknessMapping.find(item => width < item.threshold)
      return mapping ? mapping.thickness : 15
    }

    const recommendBeltWidth = getBeltWidth(paperRollCircumference)
    const recommendBeltThickness = getBeltThickness(paperRollWallThickness.value.value)

    return {
      recommendBeltLength: createParam(recommendBeltLength, 'mm'),
      recommendBeltWidth: createParam(recommendBeltWidth, 'mm'),
      recommendBeltThickness: createParam(recommendBeltThickness, 'mm'),
    }
  })

  // 机器型号预设参数配置
  const machineConfigs = computed(() => {
    return {
      [t('50_120Series')]: {
        maxWheelbase: 900,
        hubDiameter: 215,
      },
      [t('200_Series')]: {
        maxWheelbase: 1100,
        hubDiameter: 245,
      },
      [t('600_Series')]: {
        maxWheelbase: 1675,
        hubDiameter: 320,
      },
      [t('PT23-120_Series')]: {
        maxWheelbase: 900,
        hubDiameter: 240,
      },
      [t('PT23-200_Series')]: {
        maxWheelbase: 1100,
        hubDiameter: 268,
      },
    }
  })

  // 选择机器型号时更新参数
  function updateParams (machineModel: string): void {
    const config = machineConfigs.value[machineModel]
    currentSelectIndex.value = selects.value.indexOf(machineModel)
    if (config) {
      maxWheelbase.value = createParam(config.maxWheelbase, 'mm')
      hubDiameter.value = createParam(config.hubDiameter, 'mm')
    }
  }

  // 当前处于自定义模式
  const isCustomMode = computed(() => {
    return currentSelect.value === t('custom')
  })

  onMounted(() => {
    // 初始化时设置默认选择
    currentSelect.value = selects.value[0]
    updateParams(currentSelect.value)
  })

  // 监听语言变化
  watch(locale, () => {
    // 更新当前选择的机器型号
    currentSelect.value = selects.value[currentSelectIndex.value]
    updateParams(currentSelect.value)
  })

</script>
