import comfirmView from '../public/comfirm-view/comfirm-view.js'
import hintView from '../public/hint-view/hint-view.js'
import alertView from '../public/alert-view/alert-view.js'

// 设备管理类，所有方法都可以通过 this. 访问属性
class DeviceClass {
    constructor(setDeviceList) {
        this.setDeviceList = setDeviceList
        this.count_ip_x = 0
        this.count_ip_y = 0
    }

    async init() {
 
        let readResult = await window.electronAPI.runNodejsScript('json-parser', 'read', 'device_list.json')

        if (readResult.stdout === '') {
            await window.electronAPI.runNodejsScript('json-parser', 'create', 'device_list.json', JSON.stringify({devices: []}))
        } 
        else
        {
            const jsonData = JSON.parse(readResult.stdout)
            this.setDeviceList([...jsonData.data.devices])//react 替换新值要用...
        }
    }

    async handleRefresh(e, self) {
        self.startAnimation()
        this.count_ip_x = 2
        this.count_ip_y = 1

        let that = this;
        this.scanDevice(() => {
            self.stopAnimation()
        }, that)
    }

    async scanDevice(callback, that) {

        if (that.count_ip_y > 255) {
            callback()
            return
        }

        const ip = `192.168.${that.count_ip_x}.${that.count_ip_y}`
        const result = await window.electronAPI.runNodejsScript('adb-connect', ip, '5555')

        if (result.exitCode === 0) {
            that.setDeviceList(prev => [...prev, ip])
            
            console.log(`连接成功: ${ip}`)
        } else {
            console.log(`连接失败: ${ip}`)
        }

        that.count_ip_y++
        await this.scanDevice(callback, that)
    }

    async handleAdd() {
  
        const ip = document.querySelector('.ip-input input').value
        const result = await window.electronAPI.runNodejsScript('adb-connect', ip, '5555')
    
        if (result.exitCode === 0) {
            hintView.setContent('设备添加成功')
            hintView.show()

            await this.addDevice(ip)
          
            return
        }
        alertView.show()
        alertView.setContent('无法检测到连接设备')
    }

    async addDevice(ip) {
        let newArr = null
        this.setDeviceList(prev => {
            newArr = [...prev, ip]
            return newArr
        })
        await window.electronAPI.runNodejsScript('json-parser', 'update', 'device_list.json', JSON.stringify({devices: newArr}))
    }

    async removeDevice(ip) {
        
        comfirmView.show()
        comfirmView.setContent('确定要删除设备吗？')

        comfirmView.onConfirm = async () => {
            comfirmView.hide()
            
            let newArr = null
            this.setDeviceList(prev => {
                newArr = [...prev.filter(deviceIp => deviceIp !== ip)]
                return newArr
            })
            await window.electronAPI.runNodejsScript('json-parser', 'update', 'device_list.json', JSON.stringify({devices: newArr}))
        }
        comfirmView.onCancel = () => {
            comfirmView.hide()
        }
    }
}

// 导出类，由组件创建实例并管理生命周期
export { DeviceClass}
