<script lang="js" setup>
import { reactive, onMounted } from 'vue';
const state = reactive({
    value: '[100,20,69,16,55,33]',
    title: '冒泡排序可视化',
    visualSortArray: [

    ]
})


function bubbleSort(arr) {
    // 冒泡排序算法，对数组进行排序，同时记录每一步操作，保存在一个数组中
    function sort() {
        // virtualArr 用来存放 每一个步内容的数组
        const virtualArr = [arr.slice()];
        console.log('virtualArr', virtualArr)
        const max = arr.length;
        for (let i = 0; i < max; i++) {
            let done = true;
            for (let j = 0; j < max - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    let temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                    done = false;
                    virtualArr.push(arr.slice());
                }
            };
            if (done) {
                break;
            };
        }
        return virtualArr;
    }

    // 绘画，调用一次就画出一步的图像 
    function darw(arr, count) {
        const canvas = document.getElementById('myCanvas');
        const ctx = canvas.getContext('2d');
        
        // 最大高度 400
        let maxHeight = canvas.height;
        // 每个长方形的宽度
        let width = 20;
        // 每个长方形之间的间隔
        let space = 100;

        const total = arr.reduce((p, c) => p + c, 0)

        // 清空画布
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        // 设置字体
        ctx.font = "18px serif";
        // 在页面上，画出一步的内容
        for (let i = 0; i < arr.length; i++) {
            ctx.fillStyle = '#61C5FE';

            const x= i * (width + space)
            const y= maxHeight - arr[i]
            const height=Math.round((arr[i] / total)*100  + 100)
            ctx.fillRect(x, y, width, height);
            console.log('x',x)
            console.log('y',y)
            console.log('width',width)
            console.log('height',height)
            ctx.fillStyle = '#240be4';
            // 标题文字
            ctx.fillText(arr[i], x, y);
            ctx.restore();
        }

        ctx.fillText(`第${count}趟排序`, 200, 100);


        ctx.fillStyle = "rgba(0, 66, 200, 0.5)";
        ctx.beginPath();
        ctx.lineTo(0, 400);
        ctx.lineTo(800, 400);
        ctx.stroke()
    }

    // 动画 
    function animation() {
        // 调用sort 方法，返回包括每一步内容的数组
        var virtualArr = sort();
        var interval = 500;
        // 遍历得到的数组，每隔500ms，调用darw 方法，画出一步内容
        virtualArr.forEach((item, index) => {
            setTimeout(() => darw(item, index + 1), index * interval);
        });

        state.visualSortArray = virtualArr
    }

    animation();
}




const sortBtn = () => {

    const arr = state.value.replace('[', '').replace(']', '').split(',').map(n => +n)
    console.log('arr', arr)
    bubbleSort(arr);
}

onMounted(()=>{
    sortBtn()
})



</script>
<template>
    <div>

        <div style="display:flex;">
            <a-card :title="state.title" style="min-width: 800px">
                <div class="input-box">

                    <div>
                        <a-input v-model:value="state.value" placeholder="请输入数组" clearable></a-input>
                    </div>
                    <div style="margin-left:50px"><a-button type="primary" @click="sortBtn">开始排序</a-button></div>
                </div>
                <div class="container-sort">
                    <div style="text-align: right;margin-right: 20px;">
                        <div v-for="(item, index) in state.visualSortArray" :key="index">
                            <div>
                                第 {{ index + 1 }} 躺排序:  {{ item.toString() }}
                            </div>
                        </div>
                    </div>

                    <canvas id="myCanvas" width="800" height="400"> </canvas>
                </div>

            </a-card>
        </div>

    </div>
</template>

<style lang="less">
.input-box {
    display: flex;
    margin-bottom: 10px;
}

.container-sort {
    height: 800px;
    border: 1px solid #dcdcdc;
}

.box {
    margin: 10px;

    .bar {
        width: 10px;
        background: #1677ff;
        border-radius: 2px;
    }

    .num {
        font-size: 18px;
    }
}
</style>