import { onMount } from "solid-js";
import { Chart, registerShape } from '@antv/g2';
// import DataSet from '@antv/data-set';

import "../../../assets/style/BigScreenHome.css"

let cli1 = [
    "135,206,250",
    "0,191,255",
    "127,255,170",
    "0,255,127",
    "50,205,50",
    "240,230,140",
    "255,215,0",
    "255,127,80",
    "255,99,71",
    "255,0,0",
], index = 0;

// 绘制原型柱状图
registerShape('point', 'circular', {
    // 1. 定义关键点
    getPoints(cfg) {
        const x = cfg.x;
        const y = cfg.y;
        const width = cfg.size;
        return [
            { x: x - width / 2, y },
            { x, y: y + width * 2 },
            { x: x + width / 2, y },
            { x, y: y - width * 2 },
        ];
    },
    // 2. 绘制
    draw(cfg, group) {
        const points = this.parsePoints(cfg.points); // 将0-1空间的坐标转换为画布坐标
        const polygon = group.addShape('path', {
            attrs: {
                path: [
                    ['M', points[0].x, points[0].y],
                    ['L', points[1].x, points[1].y],
                    ['L', points[2].x, points[2].y],
                    ['L', points[3].x, points[3].y],
                ],
                fillOpacity: 0.95,
                fill: `l(90) 0:rgb(${cli1[index % cli1.length]}) 1:rgba(${cli1[index % cli1.length]}, 0.8)`
            },
        });
        index++
        return polygon;
    },
})

function ListeningRate(prop) {
    let $canvas = undefined;
    function TrendChart(width, height, $canvas) {
        const data = prop.list.map(v => {
            return {
                type: v.channelName,
                value: v.watchRate
            }
        }).reverse();
        const chart = new Chart({
            container: $canvas,
            width,
            height: height - 20,
            padding: [10, 60, 10, 10]
        });
        chart.data(data);
        chart.axis('value', false);
        chart.axis('type', {
            title: null,
            tickLine: null,
            subTickLine: null,
            line: null,
            label: {
                style: {
                    fill: "#fff"
                },
                formatter: () => {
                    return ""
                }
            }
        });
        // chart.tooltip({
        //     shared: true,
        //     itemTpl: `
        //     <div style="margin-bottom: 10px;list-style:none;">
        //         <span style="background-color:{color};" class="g2-tooltip-marker"></span>
        //         {value}
        //     </div>
        //     `
        // })
        chart.tooltip(false)
        chart.legend(false);
        chart.coordinate().transpose();
        chart
            .interval()
            .position('type*value')
            .color('l(0) 0:#1890ff 1:#70cdd0');
        // 添加文本标注
        data.forEach((item) => {
            chart
                .annotation()
                .text({
                    position: [item.type, item.value],
                    content: item.type,
                    style: {
                        textAlign: 'center',
                        fill: '#fff',
                    },
                    offsetX: 30,
                });
            chart
                .annotation()
                .text({
                    position: [item.type, item.value],
                    content: item.value + '%',
                    style: {
                        textAlign: 'center',
                        fill: '#fff',
                    },
                    offsetX: -30,
                });
        });
        index = 0;
        chart.point().position('type*value').shape('circular');
        chart.render();
    }
    onMount(() => {
        TrendChart(prop.width, prop.height - 10, $canvas)
    })
    return (
        <div class="livRang" style={{
            height: `${prop.height}px`
        }}>
            <div class="head">{prop.title}</div>
            <div ref={$canvas}></div>
        </div>
    );
}

export default ListeningRate;
