<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>yys Display</title>
    <style>
        .monitor-container {
            display: flex;
            flex-direction: row;
        }
        .status-container {
            display: flex;
            flex-direction: row;
        }
        .tansuo-container {
            display: flex;
            flex-direction: row;
        }
        .yaoqi-container {
            display: flex;
            flex-direction: row;
        }
        #hint_message p {
            font-size: 10px; /* 设置为较小的字体大小 */
        }
    </style>
</head>
<body>
    <h2 id="title_message">Monitor Display</h2>
    <div class="monitor-container">
        <h3>请选择时间间隔：</h3>
        <select id="timeInterval">
            <option value="500">0.5 秒</option>
            <option value="1000">1 秒</option>
            <option value="2000">2 秒</option>
            <option value="5000">5 秒</option>
            <option value="0">停止</option>
        </select>
        <canvas id="canvas" width="520" height="292"></canvas>
    </div>

    <div class="status-container ">
        <h3>状态：</h3>
        <div id="status_message" style="border: 1px solid #ccc; padding: 10px; margin-top: 10px;"></div>
        <h3>任务列表：</h3>
        <div id="hint_message"></div>
        <button class="custom-button" id="end_btn" onclick="end_script()">结束脚本</button>
        <button class="custom-button" id="resetScript_btn" onclick="reset_script()">重置脚本</button>
        <button class="custom-button" id="endgame_btn" onclick="end_game()">结束游戏</button>
        <button class="custom-button" id="restartgame_btn" onclick="restart_game()">重新关闭并自动游戏</button>
    </div>

    <div class="tansuo-container">
        <h3>章节：</h3>
        <select id="tansuo_page"></select>
        <h3>次数：</h3>
        <select id="tansuo_times"></select>
        <button class="custom-button" id="tansuo_btn" onclick="begin_tansuo()">开始探索</button>
        <button class="custom-button" id="atuo_btn" onclick="begin_auto()">自动模式</button>
    </div>
    <div class="yaoqi-container">
        <button class="custom-button" id="baigui_btn" onclick="begin_baigui()">百鬼夜行40次</button>
        <button class="custom-button" id="tupo_btn" onclick="begin_tupo()">开始突破</button>
        <button class="custom-button" id="AddPerson_btn" onclick="begin_AddPersion()">阴阳寮加人</button>
        <button class="custom-button" id="jiyang_btn" onclick="begin_jiyang()">寄养</button>
        <button class="custom-button" id="douji_btn" onclick="begin_douji()">斗技20次</button>
        <button class="custom-button" id="daoguan_btn" onclick="begin_daoguan()">道馆模式</button>
        <button class="custom-button" id="teamer_btn" onclick="begin_teamer()">队员等待</button>
        <button class="custom-button" id="yuhun_btn" onclick="begin_yuhun()">御魂100次</button>
        <button class="custom-button" id="chaogui_btn" onclick="begin_chaoguiwang()">超鬼王</button>
        <button class="custom-button" id="yeyuanhuo_btn" onclick="begin_yeyuanhuo()">业原火xxx100次</button>
        <button class="custom-button" id="zhaohuan_btn" onclick="begin_zhaohuan()">召唤100次</button>
    </div>
    
    <script src="https://cdnjs.loli.net/ajax/libs/socket.io/4.2.0/socket.io.js"></script>
    <script>
        var pageElement = document.getElementById("tansuo_page");
        // 使用循环生成下拉框选项
        for (var i = 28; i > 0; i--) {
            var option = document.createElement("option");
            option.value = i;
            option.text = i;
            pageElement.appendChild(option);
        }
        var timesElement = document.getElementById("tansuo_times");
        for (var i = 0; i <= 20; i++) {
            var option = document.createElement("option");
            option.value = i;
            option.text = i;
            timesElement.appendChild(option);
        }

        const socket = io();
        socket.on('connect', () => {
            console.log('Connected to server');
        });
        socket.on('disconnect', () => {
            console.log('Disconnected from server');
        });
        socket.on('image_data', (image_data_base64) => {
            const canvas = document.getElementById('canvas');
            const ctx = canvas.getContext('2d');
            const image = new Image();
            image.onload = () => {
                ctx.drawImage(image, 0, 0, canvas.width, canvas.height);
            };
            image.src = 'data:image/jpeg;base64,' + image_data_base64;
        });
        socket.on('processing_status', (msg) => {
            var messageBox = document.getElementById('status_message');
            messageBox.innerHTML = msg;
        });
        socket.on('processing_title', (msg) => {
            var messageBox = document.getElementById('title_message');
            messageBox.innerHTML = msg;
        });
        socket.on('processing_hint', (msg) => {
            var messageBox = document.getElementById('hint_message');
            var data = JSON.parse(msg);
            var listDiv = document.createElement("div");
            for (var i = 0; i < data.length; i++) {
                var listItem = document.createElement("p");
                listItem.textContent = data[i];
                listDiv.appendChild(listItem);
            }
            messageBox.innerHTML = "";
            messageBox.appendChild(listDiv)
            /*
            var data = JSON.parse(msg);
            for (var i = 0; i < data.length; i++) {
                var option = document.createElement("p");
                option.value = toString(i);
                option.text = data[i];
                listBox.appendChild(option);
            }*/
           
        });
        var timerId;
        function executeTask() {
            var selectElement = document.getElementById("timeInterval");
            var selectedValue = selectElement.value;
            if (selectedValue == '0'){
                clearInterval(timerId)
                var canvas = document.getElementById("canvas");
                var ctx = canvas.getContext("2d");
                ctx.clearRect(0, 0, canvas.width, canvas.height);
                timerId = setInterval(executeTask, 1000);
            }
            else
            {
                socket.emit('monitor_begin');
                clearInterval(timerId)
                timerId = setInterval(executeTask, parseInt(selectedValue));
            }
        }
        function begin_tansuo() {
            var pageNum = parseInt(document.getElementById("tansuo_page").value);
            var timesNum = parseInt(document.getElementById("tansuo_times").value);
            const data = {
                'pageNum': pageNum,
                'timesNum': timesNum
            };
            socket.emit('begin_tansuo', data);
        }

        function begin_auto() {
            var pageNum = parseInt(document.getElementById("tansuo_page").value);
            const data = {'pageNum': pageNum};
            socket.emit('begin_auto', data);
        }

        function begin_tupo() {
            socket.emit('begin_tupo');
        }


        function begin_AddPersion(){
            socket.emit('begin_AddPersion')
        }

        function begin_jiyang(){
            socket.emit('begin_jiyang')
        }

        function begin_teamer(){
            socket.emit('begin_teamer')
        }

        function begin_yuhun(){
            socket.emit('begin_yuhun')
        }

        function begin_chaoguiwang(){
            socket.emit('begin_chaoguiwang')
        }

        function begin_zhaohuan(){
            socket.emit('begin_zhaohuan')
        }

        function begin_yeyuanhuo(){
            socket.emit('begin_yeyuanhuo')
        }

        function end_script()
        {
            socket.emit('end_script');
        }

        function end_game()
        {
            socket.emit('end_game');
        }

        
        function restart_game()
        {
            socket.emit('restart_game');
        }
        function get_title()
        {
            socket.emit('get_title');
        }

        function begin_baigui(){
            socket.emit('beigin_baigui');
        }

        function begin_douji(){
            socket.emit('begin_douji');
        }

        function begin_daoguan(){
            socket.emit('begin_daoguan');
        }

        function reset_script(){
            socket.emit('reset_script');
        }
        get_title()
        executeTask()
    </script>
</body>
</html>


