export function Name() { return "WL68"; } //名称
export function VendorId() { return 0x258A; }
export function ProductId() { return 0x0049; }
export function Publisher() { return "Nollie"; } //发布者
export function Documentation() { return "gettingstarted/srgbmods-net-info"; }
export function Size() { return [16, 6]; }
export function DefaultPosition(){return [10, 100]; }
export function DefaultScale(){return 12.0}

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"},
	];
}

/* 
Time:2023/2/11
Author: Nollie(Nuonuo)
Version:V0.1
*/
const vKeys = 
[
    0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, 12, 13, 14, 15, 16, 
    17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,
	35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 
	53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
	70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86,
	87,88
];

 const vKeyPositions =[
		[0, 0],
		[0, 1],
		[1, 2],
		[1, 3],
		[1, 4],
		[0, 5],
		[1, 0],
		[1, 1],
		[2, 2],
		[2, 3],
		[2, 4],
		[1, 5],
		[2, 0],
		[2, 1],
		[3, 2],
		[3, 3],
		[3, 4],
		[2, 5],
		[3, 0],
		[3, 1],
		[4, 2],
		[4, 3],
		[4, 4],
		[3, 5],
		[4, 0],
		[4, 1],
		[5, 2],
		[5, 3],
		[5, 4],
		[4, 5],
		[5, 0],
		[5, 1],
		[6, 2],
		[6, 3],
		[6, 4],
		[5, 5],
		[6, 0],
		[6, 1],
		[7, 2],
		[7, 3],
		[7, 4],
		[6, 5],
		[7, 0],
		[7, 1],
		[8, 2],
		[8, 3],
		[8, 4],
		[7, 5],
		[8, 0],
		[8, 1],
		[9, 2],
		[9, 3],
		[9, 4],
		[8, 5],
		[9, 0],
		[9, 1],
		[10, 2],
		[10, 3],
		[10, 4],
		[10, 0],
		[10, 1],
		[11, 2],
		[11, 3],
		[11, 4],
		[11, 5],
		[10, 5],
		[11, 0],
		[11, 1],
		[12, 2],
		[12, 3],
		[12, 4],
		[12, 5],
		[12, 0],
		[12, 1],
		[13, 2],
		[13, 3],
		[13, 4],
		[13, 5],
		[14, 0],
		[14, 1],
		[14, 2],
		[14, 3],
		[14, 4],
		[14, 5],
		[15, 0],
		[15, 1],
		[15, 2],
		[15, 3],
		[15, 4]
	];


const vKeyNames = 
['ESC','~', 'TAB', 'CAPS LOCK', 'SHIFT', 'NULL5',
 "1", 'Q', 'A', 'Z', 'WIN',  "NULL13",
 '2', 'W', 'S', 'X', 'ALT_R',"NULL16",
 '3', 'E','D', 'C',"NULL22", "NULL23",
 '4', 'R', 'F', 'V',"NULL27", "NULL28",
 '5', 'T', 'G', 'B', 'SPACE', "NULL34",
 '6', 'Y', 'H', 'N', "NULL39", "NULL40",
 '7', 'U', 'J', 'M', "NULL45", "NULL46",
 '8', 'I', 'K', '<', 'ALT_L', "NULL52",
 '9', 'O', 'L', '>', 'FN', "NULL58",
 '0', 'P', ';', '/','CTRL_L', "NULL64", 
  '-', '[', '"', 'SHIFT_', "NULL69", "NULL70",
 "=", "]", "NULL73", "NULL74", "left", "NULL76",
 'BACK', '|', 'ENTER', 'ON', 'DOWN', "NULL82",
 "`", "DEL","PGUP", "PGDN", "RIGHT"
];


function initpacket1()
{

  device.log(vKeys.length, {toFile: true});
  let packet1 = [0x06,0x08,0x00,0x00,0x01,0x00,0x7a,0x01,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff];
  device.send_report(packet1,520);



}



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"},
	];
}

export function Initialize() {
	// initpacket1();

}



export function LedNames() {
	return vKeyNames;

}

export function LedPositions() {
	return vKeyPositions;
}

export function Render() {
	sendColors();
	
}

export function Shutdown() {

}

function sendColors(shutdown = false)
{
	let rgbdata = grabColors();

	let packet = [0x08,0x0a,0x7a,0x01,0x00,0x00,0x00];

	packet = packet.concat(rgbdata);	
	device.send_report(packet, 520)


}
function grabColors(shutdown = false) 
{
	let rgbdata = [];

	for(let iIdx = 0; iIdx < vKeys.length; iIdx++)
	{
		let iPxX = vKeyPositions[iIdx][0];
		let iPxY = vKeyPositions[iIdx][1];
		let color;

		if(shutdown)
		{
			color = hexToRgb(shutdownColor);
		}
		else if (LightingMode === "Forced")
		{
			color = hexToRgb(forcedColor);
		}
		else
		{
			color = device.color(iPxX, iPxY);
		}

		let iLedIdx = vKeys[iIdx] * 3;
		rgbdata[iLedIdx] = color[0];
		rgbdata[iLedIdx+1] = color[1];
		rgbdata[iLedIdx+2] = color[2];
	}

	let Fill = new Array(24).fill(0);
	rgbdata = rgbdata.concat(Fill);
	return rgbdata;
}
function hexToRgb(hex) 
{
	let result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
	let colors = [];
	colors[0] = parseInt(result[1], 16);
	colors[1] = parseInt(result[2], 16);
	colors[2] = parseInt(result[3], 16);

	return colors;
}

export function Validate(endpoint) {
	return endpoint.interface === 1 && endpoint.usage === 0x0001 && endpoint.usage_page === 0xff00;
}

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