<template>
    <div class="equip-dialog" v-if="equip">
        <div class="coins">
            {{ t('coins.0') }}:{{ useCoins }}
        </div>
        <Tooltip :infos="[t('stDesc.1'), t('stDesc.2'), t('stDesc.3'), t('stDesc.4'), t('stDesc.5')]" width="8rem">
            <div class="descript">- {{ t('stDesc.0') }} -</div>
        </Tooltip>
        <div class="equip">
            <EquipIcon :equip="equip" />
            <div class='name' :class="equip.quality.quality">
                {{ t(equip.type + '.' + equip.base.name + '.0') }}
                {{ equip.strengthenLv ? '(+' + equip.strengthenLv + ')' : '' }}
            </div>
        </div>
        <div class="entry">
            <div>
                <div v-for="v in equip.base.entry" :key="v.id">
                    {{ t(v.type + '.0') }} : + {{ strengthenValue(v.value, equip.strengthenLv) }}
                </div>
            </div>
            <div class="arror">➡</div>
            <div>
                <div v-for="v in equip.base.entry" :key="v.id">
                    <span>{{ strengthenValue(v.value, equip.strengthenLv + 1) }}</span>
                    <span class="set">
                        ⬆({{ strengthenValue(v.value, equip.strengthenLv + 1) - strengthenValue(v.value,
                            equip.strengthenLv) }})
                    </span>
                </div>
            </div>
        </div>
        <div class="btn-group" v-if='!auto'>
            <p>{{ t('stren.0') }}：<span :class="{ 'red': useCoins < needCoins }">{{ needCoins }}</span></p>
            <button class="button" @click="strengthen(false)">{{ t('stren.1') }}+{{ parseInt(equip.strengthenLv) + 1
                }}</button>
        </div>
        <div class="btn-group" v-if='!auto'>
            <p>{{ t('stren.0') }}：<span :class="{ 'red': useCoins < needCoins }">{{ needCoins }}</span></p>
            <button class="button" @click="strengthen(true)">{{ t('stren.2') }}</button>
            <span v-if="testFlag">{{ equip.strengthenLv }}<span class="arror">➡</span>{{ testResult }}</span>
        </div>
        <div class="btn-group" v-if='!auto'>
            <p>{{ t('stren.3') }}：</p>
            <p><input type="number" :placeholder="t('stren32')" max="15" min="5" v-model="autoLv"></p>
            <button class="button" @click="startAuto">{{ t('stren.4') }}</button>
        </div>
        <div class="btn-group" v-if='auto'>
            <p>{{ t('stren.5') }}...</p>
            <button class="button" @click="stopAuto">{{ t('stren.6') }}</button>
        </div>
    </div>
</template>

<script lang="ts" setup>
import { useStore } from "vuex";
import { reactive, onMounted, ref, computed, watch } from "vue";
import { useI18n } from "vue3-i18n";
import { Tooltip, EquipIcon } from "@/components"
import { strengthenValue, strengthenCoins, replace } from "@/tool"
import { extra_entry_num, weaponExtraEntry, armorExtraEntry, neckExtraEntry, ringExtraEntry, strengthen_rates, jewelryExtraEntry, pantsExtraEntry, shoesExtraEntry, bracersExtraEntry, Entry } from "@/config";
import { createt } from "@/config/i18n";
import Reforge from "./reforge.vue";

const mt = createt('backpack.');
const { t } = useI18n();
const { state, commit, dispatch } = useStore();
const auto = ref(false);
const autoLv = ref(12);
const testFlag = ref(false);
const testResult = ref(0);

const prop = defineProps({
    equip: {
        type: Object,
        default: null
    }
});

const useCoins = computed(() => {
    return state.playerAttribute.coins;
})
const needCoins = computed(() => {
    return strengthenCoins(prop.equip.lv, prop.equip.strengthenLv, prop.equip.quality.quality);
})

const strengthen = (test?) => {
    if (auto.value && prop.equip.strengthenLv >= autoLv.value) {
        auto.value = false;
        commit("set_sys_info", { msg: t('strengthenFinish'), type: "win", });
        return;
    }
    if (useCoins.value < needCoins.value) {
        auto.value = false;
        commit("set_sys_info", { msg: t('stNoCoins'), type: "warning", });
        return
    }
    const lv = prop.equip.strengthenLv;
    const len = strengthen_rates.length;
    const rate = lv < len ? strengthen_rates[lv] : strengthen_rates[len - 1];
    const result = Math.random() < rate ? 1 : -1;

    if (test) {
        testFlag.value = true;
        testResult.value = prop.equip.strengthenLv + result;
    } else {
        testFlag.value = false;
        prop.equip.strengthenLv += result;
        dispatch('saveGame');
    }

    commit("add_player_coins", -1 * needCoins.value);
    auto.value && setTimeout(strengthen, 200);
};
const startAuto = () => {
    auto.value = true;
    strengthen();
};
const stopAuto = () => {
    auto.value = false;
};

defineExpose({ stopAuto })

onMounted(() => { });
</script>
<style lang="scss" scoped>
.entry {
    width: 100%;
    padding: 1rem;
    display: flex;
    justify-content: space-around;
    font-size: 1.2rem;
    align-items: center;

    div {
        text-align: left;

        &>div {
            height: 2rem;
        }

        .set {
            color: #2fe20f;
            margin-left: 0.7rem;
        }
    }
}

.btn-group {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0.5rem;
    background: #000;

    .red {
        color: red;
    }
}

</style>