export function Name() { return "Asus Aura Terminal"; }
export function VendorId() { return 0x0b05; }
export function ProductId() { return 0x1889; }
export function Publisher() { return "WhirlwindFX"; }
export function Size() { return [5, 1]; }
export function DefaultPosition(){ return [0, 0]; }
export function DefaultScale(){ return 1.0; }
export function Type() { return "Hid"; }
export function SupportsSubdevices(){ return true; }
export function DefaultComponentBrand() { return "CompGen"; }
/* global
shutdownColor:readonly
LightingMode:readonly
forcedColor:readonly
*/
export function ControllableParameters(){
	return [
		{"property":"shutdownColor", "group":"lighting", "label":"Shutdown Color", "min":"0", "max":"360", "type":"color", "default":"#009bde"},
		{"property":"LightingMode", "group":"lighting", "label":"Lighting Mode", "type":"combobox", "values":["Canvas", "Forced"], "default":"Canvas"},
		{"property":"forcedColor", "group":"lighting", "label":"Forced Color", "min":"0", "max":"360", "type":"color", "default":"#009bde"},
		{"property":"HWL_mode", "group":"", "label":"切换控制盒模式", "type":"combobox", "values":["DRGB", "AURA", "ICUE8", "ICUE16", "ICUE24", "ICUE30", "ICUEBUT"], "default":"AURA", "tooltip":"切换控制盒模式DRGB__华硕AURA__ICUE8/16/24/30识别为commander__ICUEBUT恢复拨码调整模式!"},

	];
}

const DeviceMaxLedLimit = 361;
const ChannelArray =
[
	["Channel 1", 90],
	["Channel 2", 90],
	["Channel 3", 90],
	["Channel 4", 90],
	["Channel 5LOGO", 1],

];

function SetupChannels() {
	device.SetLedLimit(DeviceMaxLedLimit);

	for(let i = 0; i < ChannelArray.length; i++) {
		device.addChannel(ChannelArray[i][0], ChannelArray[i][1]);
	}
}

const vKeyNames = [];
const vKeyPositions = [];
const MaxLedsInPacket = 20;

let lastHWLchange = 0;
let HWLupdateRequested = false;

export function LedNames() {
	return vKeyNames;
}

export function LedPositions() {
	return vKeyPositions;
}
export function Initialize() {
	SetupChannels();
	//requestFirmwareVersion();
}


function sendZone(Channel, shutdown = false) {
	const componentChannel = device.channel(ChannelArray[Channel][0]);
	let ChannelLedCount = componentChannel.ledCount > ChannelArray[Channel][1] ? ChannelArray[Channel][1] : componentChannel.ledCount;
	let RGBData = [];


	if(!ChannelLedCount){
		ChannelLedCount = 40;
		let pulseColor = device.getChannelPulseColor(ChannelArray[Channel][0]);
		RGBData = device.createColorArray(pulseColor, ChannelLedCount, "Inline");
	}else if(shutdown) {
		RGBData = device.createColorArray(shutdownColor, ChannelLedCount, "Inline");
	} else if(LightingMode === "Forced") {
		RGBData = device.createColorArray(forcedColor, ChannelLedCount, "Inline");
	} else {
		RGBData = componentChannel.getColors("Inline");
	}

	const NumPackets = Math.ceil(ChannelLedCount / MaxLedsInPacket);
	for(let CurrPacket = 0; CurrPacket < NumPackets; CurrPacket++) {
	let packet = [0xEC,0x40,Channel,];
	packet[3] = (0x14 * CurrPacket);
	packet[4] = 0x14;
	if(CurrPacket == NumPackets-1){
	packet[2] = (128 + Channel);	
	packet[3] = (20 * CurrPacket);
	packet[4] = ChannelLedCount % MaxLedsInPacket == 0 ? 0x14 : ChannelLedCount % MaxLedsInPacket;
		}
		packet = packet.concat(RGBData.splice(0, 60));
		device.write(packet, 65);
	
	}
}

export function Render() {
	sendZone(0);
	sendZone(1);
	sendZone(2);
	sendZone(3);
	sendZone(4);
}

export function Shutdown() {
	sendZone(0, true);
	sendZone(1, true);
	sendZone(2, true);
	sendZone(3, true);
	sendZone(4, true);
}

export function Validate(endpoint) {
	return endpoint.interface === 2 || endpoint.interface === -1;
}

function hexToRgb(hex) {
	const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
	const colors = [];
	colors[0] = parseInt(result[1], 16);
	colors[1] = parseInt(result[2], 16);
	colors[2] = parseInt(result[3], 16);

	return colors;
}



export function onHWL_modeChanged(){ HWL_setting_mode(); }

function HWL_setting_mode() {
	let hwlmode;		//控制盒模式
	if(HWL_mode == "DRGB") { hwlmode = 0x01; } else if(HWL_mode == "AURA") { hwlmode = 0x02; } else if(HWL_mode == "ICUE8") { hwlmode = 0x08; } else if(HWL_mode == "ICUE16") { hwlmode = 0x10; } else if(HWL_mode == "ICUE24") { hwlmode = 0x18; } else if(HWL_mode == "ICUE30") { hwlmode = 0x1E; }else if(HWL_mode == "ICUEBUT") { hwlmode = 0xF0; } 
	const packet = [0x00, 0x45, 0x35, hwlmode];
	device.write(packet, 65);
}

export function ImageUrl() {
	return "https://gitee.com/drgb-controller/drgb/raw/master/Image/AURA_TERMINAL.png"
}