export function Name() { return "YICO 16"; }
export function VendorId() { return 0x2486; }
export function ProductId() { return [0x1616]; }
export function Publisher() { return "YICO"; }
export function Size() { return [1, 1]; }
export function DefaultPosition(){ return [160, 100]; }
export function DefaultScale(){ return 1.0; }
export function Type() { return "Hid"; }
export function DeviceType(){return "lightingcontroller";}
export function DefaultComponentBrand() { return "CompGen"; }
/* global
shutdownColor:readonly
LightingMode:readonly
forcedColor:readonly
seek:readonly
Turbo:readonly
*/
export function ControllableParameters() {
	return [
		{"property":"seek", "group":"lighting", "label":"呼吸定位设备", description:"启用后，未添加模型的接口单色呼吸，根据接口颜色定位设备,设置完成后推荐关闭!推荐关闭！","type":"boolean", "default":"true"},
		{"property":"Turbo", "group":"lighting", "label":"高刷新率", description:"启用后，提升刷新率，但会占用更多系统资源,玩游戏等高负载使用时推荐关闭!！", "type":"boolean", "default":"true"},
//		{"property":"LightingMode", "group":"lighting", "label":"模式", "type":"combobox", "values":["Canvas", "Forced"], "default":"Canvas", "tooltip":"Canvas：画布模式__Forced：单色模式!"},
//		{"property":"forcedColor", "group":"lighting", "label":"单色", "min":"0", "max":"360", "type":"color", "default":"#FF0000"},

			];
}

export function SubdeviceController() { return true; }

const ProductNames = {
	0x1616: "YICO 16 ELITE",

};
const ChannelALed = 256;
const ChannelBLed = 256;

const DeviceMaxLedLimit = 4096;
const ChannelArray =
[
	["Channel A1", ChannelALed],
	["Channel A2", ChannelALed],
	["Channel A3", ChannelALed],
	["Channel A4", ChannelALed],
	["Channel A5", ChannelALed],
	["Channel A6", ChannelALed],
	["Channel A7", ChannelALed],
	["Channel A8", ChannelALed],

	["Channel B1", ChannelBLed],
	["Channel B2", ChannelBLed],
	["Channel B3", ChannelBLed],
	["Channel B4", ChannelBLed],
	["Channel B5", ChannelBLed],
	["Channel B6", ChannelBLed],
	["Channel B7", ChannelBLed],
	["Channel B8", ChannelBLed],

];

function SetupChannels() {
	device.SetLedLimit(DeviceMaxLedLimit);
	for(let i = 0; i < MChannel; i++) {
		device.addChannel(ChannelArray[i][0], ChannelArray[i][1]);
	}
	
}

const ChannelLedNum =  256;
const MaxLedsInPacket = 0x0154;
const MaxPacket = 0x0100;
const MaxOneLeds = 0x013C;
const MaxLeds = 256;
let MChannel = 16;

const vKeyNames = [];
const vKeyPositions = [];

export function LedNames() {
	return vKeyNames;
}

export function LedPositions() {
	return vKeyPositions;
}

export function Initialize() {
	device.setName(ProductNames[device.productId()]);
	SetupChannels();
	requestFirmwareVersion();
	if(Turbo) device.setFrameRateTarget(60);

}


export function Render() {
	SendChannel(0);
}

function compareFirmwareVersion() {
	const firmwarePacket = device.read([0x00],  6 );
	const FirmwareVersion = firmwarePacket[2] + "." + firmwarePacket[3] + "." + firmwarePacket[4]+ "." + firmwarePacket[5];
	device.log("DRGB Firmware version: " + FirmwareVersion);
}

function requestFirmwareVersion() {
	const packet = [ 0x00, 0x02 ];
	device.write(packet, 65);
	compareFirmwareVersion();
}


function SendChannel(Channel, overrideColor) {
	let ArrayData = [];
	let RGBData = [];
	let LedCount = 0;

	for(let Channel = 0; Channel < MChannel; Channel++) {
	let ChannelData = [];
	const componentChannel = device.channel(ChannelArray[Channel][0]);
	let ChannelLedCount = componentChannel.ledCount > MaxLeds ? MaxLeds : componentChannel.ledCount;

	if(overrideColor) {
		ChannelData = device.createColorArray(shutdownColor, ChannelLedCount, "Inline");
	}
	//  else if(LightingMode === "Forced") {
	// 	ChannelData = device.createColorArray(forcedColor, ChannelLedCount, "Inline");
	// } 
	else if(componentChannel.shouldPulseColors()) {
		if(seek){
			ChannelLedCount = ChannelLedNum/2;
		}else{
			ChannelLedCount = 5;
		}
		const pulseColor = device.getChannelPulseColor(ChannelArray[Channel][0], ChannelLedCount);
		ChannelData = device.createColorArray(pulseColor, ChannelLedCount, "Inline");
	} else {
		ChannelData = componentChannel.getColors("Inline");
	}

	LedCount += ChannelLedCount;
	let QLedCount = (ChannelLedCount & 0xFFFF) >> 8;
	let PLedCount = ChannelLedCount & 0xFF;
	ArrayData.push(QLedCount,PLedCount);
	RGBData.push(...ChannelData.slice(0,ChannelLedCount*3));
   }
   if (MChannel<36) ArrayData[71] = 0;
   ArrayData.push(...RGBData);
   let RGBLedCount = ArrayData.length;
   	const NumPackets = Math.ceil(RGBLedCount / 1020) + 99;
	let HigCount = LedCount/MaxPacket >= 1 ? 1 : 0;
	let LowCount = LedCount >= MaxOneLeds ? 60 : LedCount%MaxPacket; 
	LedCount = LedCount <= MaxOneLeds ? 0 : LedCount-MaxOneLeds;
	for(let CurrPacket = 100; CurrPacket <= NumPackets; CurrPacket++) {
		let packet = [0x00, CurrPacket,NumPackets,HigCount,LowCount];
		packet = packet.concat(ArrayData.splice(0, 1020));
		device.write(packet, 1025);
		if(LedCount){
			HigCount = LedCount / MaxPacket >= 1 ? 1 : 0;
			LowCount = LedCount >= MaxLedsInPacket ? 84 : LedCount%MaxPacket;
			LedCount = LedCount <= MaxLedsInPacket ? 0 : LedCount-MaxLedsInPacket;
		}
	}
}

export function Shutdown(SystemSuspending) {
	const color = SystemSuspending ? "#000000" : shutdownColor;
	SendChannel(0, color);
}

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 onTurboChanged(){ 
	if(Turbo){
		device.setFrameRateTarget(60);
	}else{
		device.setFrameRateTarget(30);
	}
}

export function Validate(endpoint) {
	return endpoint.interface === 0 || endpoint.interface === -1; // && endpoint.usage === 0x0002 && endpoint.usage_page === 0xFF00;
}

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