/*
 * Copyright (c) 2025 和丰山海
 * https://www.howfor.com
 * 020-28186708
 */
package com.howfor.ys3566demo

import android.os.Bundle
import android.os.Environment
import android.util.Log
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat


class IndexActivity : AppCompatActivity() {
    val TAG: String = "howfor"
    //1.初始化实例
    private lateinit var api: MyManager;
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //enableEdgeToEdge()
        setContentView(R.layout.activity_index)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        //1.初始化实例
        api = MyManager.getInstance(this);
        //2.绑定AIDL服务
        api.bindAIDLService(this);
        //3.设置AIDL回调
        api.setConnectClickInterface {
            //回调成功,可以正常调用接口了
            //例如:mapi. getApiVersion();
            System.out.println("apiversion:" + api. getApiVersion());
        }

        val txtEthMac = findViewById<TextView>(R.id.txtEthMac);
        txtEthMac.text = "以太网mac地址:" + api.ethMacAddress;

        // 关机,重启,截屏
        val btnShutdown = findViewById<Button>(R.id.btnShutdown);
        val btnReboot = findViewById<Button>(R.id.btnReboot);
        val btnScreenshot = findViewById<Button>(R.id.btnScreenshot);
        btnShutdown.setOnClickListener {
            // 添加关机逻辑
            api.shutdown()
        }

        btnReboot.setOnClickListener {
            // 添加重启逻辑
            api.reboot()
        }
        btnScreenshot.setOnClickListener {
            // 截屏
            api.takeScreenshot(Environment.getExternalStorageDirectory().path +"/Pictures/screenshot.jpg");
        }

        // 红,绿,蓝,黄,紫,浅蓝,粉,关灯
        val btnRed = findViewById<Button>(R.id.btnRed);
        val btnGreen = findViewById<Button>(R.id.btnGreen);
        val btnBlue = findViewById<Button>(R.id.btnBlue);
        val btnYellow = findViewById<Button>(R.id.btnYellow);
        val btnPurple = findViewById<Button>(R.id.btnPurple);
        val btnLightBlue = findViewById<Button>(R.id.btnLightBlue);
        val btnPink = findViewById<Button>(R.id.btnPink);
        val btnClose = findViewById<Button>(R.id.btnClose);
        btnRed.setOnClickListener {
            api.writeGpioValue(1, "0");//红色
            api.writeGpioValue(2, "0");//绿色
            api.writeGpioValue(3, "0");//蓝色
            api.writeGpioValue(1, "1");
        }
        btnGreen.setOnClickListener {
            api.writeGpioValue(1, "0");//红色
            api.writeGpioValue(2, "0");//绿色
            api.writeGpioValue(3, "0");//蓝色

            api.writeGpioValue(2, "1");
        }
        btnBlue.setOnClickListener {
            api.writeGpioValue(1, "0");//红色
            api.writeGpioValue(2, "0");//绿色
            api.writeGpioValue(3, "0");//蓝色

            api.writeGpioValue(3, "1");
        }
        btnYellow.setOnClickListener {
            api.writeGpioValue(1, "0");//红色
            api.writeGpioValue(2, "0");//绿色
            api.writeGpioValue(3, "0");//蓝色

            api.writeGpioValue(1, "1");
            api.writeGpioValue(2, "1");
        }
        btnPurple.setOnClickListener {
            api.writeGpioValue(1, "0");//红色
            api.writeGpioValue(2, "0");//绿色
            api.writeGpioValue(3, "0");//蓝色

            api.writeGpioValue(1, "1");
            api.writeGpioValue(3, "1");
        }
        btnLightBlue.setOnClickListener {
            api.writeGpioValue(1, "0");//红色
            api.writeGpioValue(2, "0");//绿色
            api.writeGpioValue(3, "0");//蓝色

            api.writeGpioValue(2, "1");
            api.writeGpioValue(3, "1");
        }
        btnPink.setOnClickListener {
            api.writeGpioValue(1, "0");//红色
            api.writeGpioValue(2, "0");//绿色
            api.writeGpioValue(3, "0");//蓝色

            api.writeGpioValue(1, "1");
            api.writeGpioValue(2, "1");
            api.writeGpioValue(3, "1");
        }
        btnClose.setOnClickListener {
            api.writeGpioValue(1, "0");//红色
            api.writeGpioValue(2, "0");//绿色
            api.writeGpioValue(3, "0");//蓝色
        }

        // 继电器开关
        val btnRelayOn = findViewById<Button>(R.id.btnRelayOn);
        val btnRelayOff = findViewById<Button>(R.id.btnRelayOff);
        btnRelayOn.setOnClickListener {
            api.writeGpioValue(41, "1");//NO与COM断开
        }
        btnRelayOff.setOnClickListener {
            api.writeGpioValue(41, "0");//NO与COM联通
        }

        // 定时开关机测试
        val btnPowerOnOffWithWeekly = findViewById<Button>(R.id.btnPowerOnOffWithWeekly);
        btnPowerOnOffWithWeekly.setOnClickListener {
            // 此方法一天只能有一组时间设入,并且开机时间在前,关机时间在后
            // 周模式设置定时开关机,一天只有一组开关机时间
            //api.setPowerOnOffWithWeekly(timeonArray,timeoffArray,weekdays);

            /* java方法
            int [] timeonArray = new int{8,30};
            int [] timeoffArray = new int{18,30};
            Int[] weekdays = new int{1,1,1,1,1,0,0};//周一到周日工作状态,1为开机,0为不开机
            api.setPowerOnOffWithWeekly(timeonArray,timeoffArray,weekdays);
             */

            /* kt方法
            val timeonArray = intArrayOf(8, 30);
            val timeoffArray = intArrayOf(18, 30)
            val weekdays = intArrayOf(1, 1, 1, 1, 1, 0, 0) // 周一到周日工作状态, 1为开机,0为不开机
            //设置上述时间将会在每周一到周五的8:30开机,18:30关机
            api.setPowerOnOffWithWeekly(timeonArray,timeoffArray,weekdays);
            */
        }

        // 开机自启
        val btnBoot = findViewById<Button>(R.id.btnBoot);
        btnBoot.setOnClickListener {
            // 本应用已实现开机自启
            // 详细请参考https://blog.51cto.com/hefengshanhai/10995532
            Log.d(TAG, "开机自启")
        }

        // RS485测试
        val btnRS485 = findViewById<Button>(R.id.btnRS485);
        btnRS485.setOnClickListener {
            Log.d(TAG, "RS485测试")
            // 串口开发需要用户自己实现串口读写,此处仅做示例,需要您自己实现串口开发啊
            // 可使用串口调试助手测试如(ComAssistant)
            // 波特率:9600
            // 串口:/dev/ttyS0
            // 开门命令:FA01010101000000E21D
            // 关门命令:FA010101010000ffa25d
        }

    }
}