export function Name() { return "YICO"; }
export function VendorId() { return 0x1368; }
export function ProductId() { return [0x6077,0x6078,0x6079]; }
export function Publisher() { return "YICO"; }
export function Size() { return [0, 0]; }
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
ColorCompression: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"},
		{"property":"ColorCompression_enable", "group":"lighting", "label":"启用颜色压缩（减少光谱并提高帧速率）", description:"推荐开启,设备灯珠越多效果越明显好", "type":"boolean", "default":"true"},

			];
}

export function SubdeviceController(){ return true; }

const ProductNames = {
	0x6077: "YICO 8 ELITE",
	0x6078: "YICO 08 ELITE",
	0x6079: "YICO 08 ELITE",
};

const ChannelALed = 256;
const ChannelBLed = 240;
const DeviceMaxLedLimit = 1024;
const ChannelArray =
[
	["Channel 1", ChannelALed],
	["Channel 2", ChannelALed],
	["Channel 3", ChannelALed],
	["Channel 4", ChannelALed],
	["Channel 5", ChannelBLed],
	["Channel 6", ChannelBLed],
	["Channel 7", ChannelBLed],
	["Channel 8", ChannelBLed],
	["Channel 9", ChannelBLed],
	["Channel 10", ChannelBLed],
	["Channel 11", ChannelBLed],
	["Channel 12", ChannelBLed],
];

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

const MaxLedsInPacket = 21;
let ChannelMaxLed =  256;
const d8 = 24697;
let MChannel = 8;
let version = 0;
let array_head = 0x46;
let buf_head = 0x47;
let buf_end = 92;
const vKeyNames = [];
const vKeyPositions = [];

export function LedNames() {
	return vKeyNames;
}

export function LedPositions() {
	return vKeyPositions;
}

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

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

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

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

function SendChannel(Channel, overrideColor) {
	let RGBData = [];
	let ArrayData = [];
	const multiplier = ColorCompression_enable ? 2 : 1;
	const compressedRGB = [];

	for(let Channel = 0; Channel < MChannel; Channel++) {
	let ChannelData = [];
	const componentChannel = device.channel(ChannelArray[Channel][0]);
	let ChannelLedCount = componentChannel.ledCount > ChannelMaxLed ? ChannelMaxLed : 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 = 120;
		}else{
			ChannelLedCount = 5;
		}
		const pulseColor = device.getChannelPulseColor(ChannelArray[Channel][0], ChannelLedCount);
		ChannelData = device.createColorArray(pulseColor, ChannelLedCount, "Inline");
	} else {
		ChannelData = componentChannel.getColors("Inline");
	}

	let QLedCount = (ChannelLedCount & 0xFFFF) >> 8;
	let PLedCount = ChannelLedCount & 0xFF;
	ArrayData.push(QLedCount,PLedCount);
	RGBData.push(...ChannelData.slice(0,ChannelLedCount*3));
	}

	let RGBLedCount = RGBData.length / 3 ;
	if(ColorCompression_enable) {
		for(let runCount = 0; runCount < RGBLedCount / multiplier; runCount++) {
			compressedRGB[(runCount*3)] = (((RGBData[(runCount*6)] & 0xFF) >> 4) | ((((RGBData[(runCount*6)+1] & 0xFF) >> 4) & 0xFF) << 4));
			compressedRGB[(runCount*3)+1] = (((RGBData[(runCount*6)+2] & 0xFF) >> 4) | ((((RGBData[(runCount*6)+3] & 0xFF) >> 4) & 0xFF) << 4));
			compressedRGB[(runCount*3)+2] = (((RGBData[(runCount*6)+4] & 0xFF) >> 4) | ((((RGBData[(runCount*6)+5] & 0xFF) >> 4) & 0xFF) << 4));
		}
	}

	const NumPackets = Math.ceil(RGBLedCount / MaxLedsInPacket / multiplier) + 100;
	const compressionenable = ColorCompression_enable == true ? 0xAA : 0xBB;
	let Arraypacket = [0x00,0x60,compressionenable];
	Arraypacket = Arraypacket.concat(ArrayData);
	device.write(Arraypacket, 65);
	for(let CurrPacket = 100; CurrPacket < NumPackets; CurrPacket++) {
		CurrPacket = CurrPacket == (NumPackets-1) ? CurrPacket+100 : CurrPacket;
		let packet = [0x00, CurrPacket];
		packet = packet.concat(ColorCompression_enable ? compressedRGB.splice(0, 63) : RGBData.splice(0, 63));
		device.write(packet, 65);
	}
}

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 === 2 || endpoint.interface === -1;
}

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