<script setup lang="ts">
import { ref, watch } from 'vue'
import DesignStage from '../../../components/DesignStage.vue'
import type Player from '../../../commons/player.ts'
import { cssAssetUrl } from '@/commons/assets.ts';

const props = defineProps<{
    rankList: Player[];
}>();

const emit = defineEmits<{
    next: [];
    back: [];
}>();

const FINAL_RANK_LIMIT = 10;
const imageUrls = {
    bg: cssAssetUrl('game1/bg.png'),
    logo: cssAssetUrl('game1/logo.png'),
    title: cssAssetUrl('game1/title3.png'),
    title2: cssAssetUrl('game1/title4.png'),
    podium: cssAssetUrl('game1/podium.png'),
    fireworks: cssAssetUrl('game1/fireworks.png'),
    avatar: cssAssetUrl('avatar.png'),
    guan1: cssAssetUrl('game1/guan1.png'),
    pai1: cssAssetUrl('game1/pai1.png'),
    guan2: cssAssetUrl('game1/guan2.png'),
    pai2: cssAssetUrl('game1/pai2.png'),
    guan3: cssAssetUrl('game1/guan3.png'),
    pai3: cssAssetUrl('game1/pai3.png'),
    rankIcon: cssAssetUrl('game1/no-icon.png'),
    avatarAnimation: cssAssetUrl('avatar_animation.png'),
    next: cssAssetUrl('game1/next.png'),
    back: cssAssetUrl('game1/back.png'),
    vip: cssAssetUrl('game1/vip.png'),
};

const emptyPlayer = (index: number): Player => ({
    active: false,
    wechat_id: `empty-${index}`,
    nickname: '虚位以待',
    avatar: '',
    score: 0,
});

const list = ref<Player[]>(Array.from({ length: FINAL_RANK_LIMIT }, (_, index) => emptyPlayer(index)));

const updateFinalRank = (rankList: Player[]) => {
    list.value = Array.from(
        { length: FINAL_RANK_LIMIT },
        (_, index) => rankList[index] ?? emptyPlayer(index),
    );
};

watch(
    () => props.rankList,
    (rankList) => updateFinalRank(rankList),
    { immediate: true, deep: true },
);

const renderAvatar = (item: any | null) => {
    if (item && item.avatar) {
        return `background: url('${item.avatar}') center / cover no-repeat;`
    }
    return `background: ${imageUrls.avatar} center / cover no-repeat;`
}
</script>

<template>
    <DesignStage>
        <div class="bg">
            <div class="img-logo"></div>
            <div class="img-title"></div>
            <div class="img-title2"></div>

            <div class="podium">
                <div class="rank-vip"></div>
                <div class="rank-no1">
                    <div class="gu-burst"></div>
                    <div class="avatar" :style="`${renderAvatar(list[0])}`">
                    </div>
                    <div class="nickname">{{ list[0]?.nickname }}</div>
                </div>
                <div class="rank-no2">
                    <div class="gu-burst"></div>
                    <div class="avatar" :style="`${renderAvatar(list[1])}`">
                    </div>
                    <div class="nickname">{{ list[1]?.nickname }}</div>
                </div>
                <div class="rank-no3">
                    <div class="gu-burst"></div>
                    <div class="avatar" :style="`${renderAvatar(list[2])}`">
                    </div>
                    <div class="nickname">{{ list[2]?.nickname }}</div>
                </div>
            </div>

            <div class="rank-container">
                <div v-for="(item, index) in list.slice(3)" :key="item.userid || item.wechat_id || `rank-${index}`">
                    <div class="no-icon">{{ index + 4 }}</div>
                    <div>
                        <div class="img-avatar-container">
                            <div class="img-avatar" :style="`${renderAvatar(list[index + 3])}`"></div>
                        </div>
                        <div class="txt-nickname">{{ item.nickname }}</div>
                    </div>
                </div>
            </div>

            <div class="btn-next" @click="emit('next')"></div>
            <div class="btn-back" @click="emit('back')"></div>
        </div>
    </DesignStage>
</template>

<style scoped>
.bg {
    width: 1920px;
    height: 1080px;
    background: v-bind('imageUrls.bg') center / cover no-repeat;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    position: relative;
}

.img-logo {
    background: v-bind('imageUrls.logo');
    background-size: cover;
    width: 428px;
    height: 77px;
    position: absolute;
    left: 37px;
    top: 82px;
}

.img-title {
    background: v-bind('imageUrls.title');
    width: 967px;
    height: 122px;
    margin: 0 auto;
    margin-top: 37px;
}

.img-title2 {
    background: v-bind('imageUrls.title2') no-repeat center;
    width: 457px;
    height: 57px;
    margin: 0 auto;
    margin-top: 37px;
}

.podium {
    background: v-bind('imageUrls.podium') no-repeat center;
    width: 1920px;
    height: 740px;
    position: absolute;
    bottom: 0;

    .gu-burst {
        width: 200px;
        height: 200px;
        position: absolute;
        z-index: 1;
        border-radius: 50%;
        opacity: 1;
        transform: scale(0.6);
        transform-origin: center center;
        background: v-bind('imageUrls.fireworks') center / cover no-repeat;
        pointer-events: none;
        animation: gu-burst-expand 1.2s ease-out infinite;
    }

    .rank-no1 {
        position: relative;
        left: 308px;
        top: 150px;

        .gu-burst {
            left: 580px;
            top: -200px;
        }

        .avatar {
            width: 160px;
            height: 160px;
            border-radius: 80px;
            position: absolute;
            left: 575px;
            top: -146px;
        }

        .nickname {
            width: 167px;
            height: 46px;
            background: linear-gradient(0deg, #FFBF5A 0%, #FFE47A 100%);
            border: 1px solid #FFF9DC;
            color: #4B1A14;
            text-align: center;
            line-height: 46px;
            position: absolute;
            left: 570px;
            top: -25px;
            z-index: 1;
            border-radius: 23px;
        }
    }

    .rank-no2 {
        position: relative;
        left: 308px;
        top: 150px;
        .gu-burst {
            left: 140px;
            top: -170px;
        }

        .avatar {
            width: 144px;
            height: 144px;
            border-radius: 72px;
            position: absolute;
            left: 180px;
            top: -113px;
        }


        .nickname {
            width: 167px;
            height: 46px;
            background: linear-gradient(0deg, #AEA6A7 0%, #EAE4DF 100%);
            border: 1px solid white;
            color: #383636;
            text-align: center;
            line-height: 46px;
            position: absolute;
            left: 165px;
            top: 2px;
            z-index: 1;
            border-radius: 23px;
        }
    }

    .rank-no3 {
        position: relative;
        left: 308px;
        top: 150px;
        .gu-burst {
            left: 1020px;
            top: -170px;
        }

        .avatar {
            width: 144px;
            height: 144px;
            border-radius: 72px;
            position: absolute;
            left: 987px;
            top: -116px;
        }

        .nickname {
            width: 167px;
            height: 46px;
            background: linear-gradient(0deg, #F3AE76 0%, #F9CDA3 100%);
            color: #6F3A0E;
            border: 1px solid #FEFEFD;
            text-align: center;
            line-height: 46px;
            position: absolute;
            left: 977px;
            top: 2px;
            z-index: 1;
            border-radius: 23px;
        }


    }
}

.rank-vip {
    width: 969px;
    height: 238px;
    background: v-bind('imageUrls.vip');
    position: absolute;
    left: 480px;
    top: -50px;
}

.rank-container {
    display: flex;
    align-items: center;
    width: 1270px;
    height: 115px;
    position: absolute;
    bottom: 243px;
    left: 325px;

    >div {
        flex: 1;
        display: grid;
        width: 100px;
        height: 100%;

        .no-icon {
            background: v-bind('imageUrls.rankIcon') no-repeat center;
            width: 40px;
            height: 40px;
            position: absolute;
            text-align: center;
            line-height: 40px;
            color: white;
            font-weight: bold;
        }

        .img-avatar-container {
            position: relative;
            margin: 0 auto;
            margin-top: 10px;
            width: 72px;
            height: 72px;
            border-radius: 36px;
        }

        .img-avatar-container::before {
            content: "";
            position: absolute;
            inset: 0;
            border-radius: inherit;
            transform-origin: center center;
            background: v-bind('imageUrls.avatarAnimation') center / cover no-repeat;
            animation: avatar-rotate 10s linear infinite;
        }

        .img-avatar {
            position: absolute;
            left: 50%;
            top: 50%;
            z-index: 1;
            width: 60px;
            height: 60px;
            border-radius: 30px;
            transform: translate(-50%, -50%);
            background: v-bind('imageUrls.avatar') center / cover no-repeat;
        }

        .txt-nickname {
            color: white;
            font-size: 20px;
            text-align: center;
        }
    }
}

.btn-next {
    position: absolute;
    bottom: 100px;
    left: 620px;
    width: 316px;
    height: 105px;
    cursor: pointer;
    background: v-bind('imageUrls.next') center / cover no-repeat;
}

.btn-back {
    position: absolute;
    bottom: 100px;
    left: 1020px;
    width: 316px;
    height: 105px;
    cursor: pointer;
    background: v-bind('imageUrls.back') center / cover no-repeat;
}
</style>
