export function Name() { return "DK100"; } //名称
export function VendorId() { return 0x05AC; }
export function ProductId() { return 0x024F; }
export function Publisher() { return "Nollie"; } //发布者
export function Documentation() { return "gettingstarted/srgbmods-net-info"; }
export function Size() { return [33, 11]; }
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"},
	];
}

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, 89, 90, 91, 92, 93, 94,95 ,96,97,98,99,100,101
               ,102,103,104,105,106,107,108,109,110,111,112,113
];
const vKeyPositions = 
[[0, 0], [0, 2], [1, 4], [1, 6], [2, 8], [1, 10], 
[2, 0], [2, 2], [3, 4], [3, 6], [4, 8], [3, 10],
 [4, 0], [4, 2], [5, 4], [5, 6], [6, 8], [5, 10], 
[6, 0], [6, 2], [7, 4], [7, 6], [8, 8], [7, 10], 
[8, 0], [8, 2], [9, 4], [9, 6], [10, 8], [9, 10], 
[10, 0], [10, 2], [11, 4], [11, 6], [12, 8], [11, 10], 
[12, 0], [12, 2], [13, 4], [13, 6], [14, 8], [13, 10], 
[14, 0], [14, 2], [15, 4], [15, 6], [16, 8], [15, 10],
 [16, 0], [16, 2], [17, 4], [17, 6], [18, 8], [19, 10], 
[18, 0], [18, 2], [19, 4], [19, 6], [20, 8], [21, 10],
 [20, 0], [20, 2], [21, 4], [21, 6], [22, 8], [23, 10], 
[22, 0], [22, 2], [23, 4], [23, 6], [23, 8], [24, 10], 
[24, 0], [24, 2], [25, 4], [25, 6], [24, 8], [25, 10], 
[26, 0], [27, 2], [27, 4], [26, 6], [25, 8], [27, 10],
 [28, 0], [28, 2], [28, 4], [27, 6], [28, 8], [28, 10], 
[29, 0], [29, 2], [29, 4], [29, 6], [29, 8], [29, 10], 
[30, 0], [30, 2], [30, 4], [30, 6], [30, 8], [30, 10], 
[31, 0], [31, 2], [31, 4], [31, 6], [31, 8], [31, 10], 
[32, 0], [32, 2], [32, 4], [32, 6], [32, 8], [32, 10]];


const vKeyNames = 
[
'ESC', '~', 'TAB', 'CAPS LOCK', 'SHIFT_L', 'CTRL-L', 'F1', '1', 'Q', 'A', 'Z', 
'ALT_L', 'F2', '2', 'W', 'S', 'X', 'WIN', 'F3', '3', 'E',
 'D', 'C', 'NULL1', 'F4', '4', 'R', 'F', 'V', 'NULL2', 'F5', 
 '5', 'T', 'G', 'B', 'SPACE', 'F6', '6', 'Y', 'H', 'N', 'NULL3',
 'F7', '7', 'U', 'J', 'M', 'NULL4', 'F8', '8', 'I', 'K', '<', 
 'ALT_R', 'F9', '9', 'O', 'L', '>', 'FN', 'F10', '0', 'P', ';', '?',
 'CTRL_R', 'F11', '-', '[', '"', 'NULL5', 'NULL6', 'F12', '=', ']', 'NULL7', 'NULL8', 
 'NULL9', 'PRTSC', 'BACK', '|', 'ENTER', 'SHIFT_R', 'LEFT', 'DEL', 'NULL10', 'NULL11', 'NULL12', 'UP', 'DOWN',
 'HOME', 'NUM', 'SEVEN', 'FOUR', 'ONE', 'RIGHT', 'INS', '/', 'EIGHT', 'FIVE', 'TWO', 'ZERO',
 'PGUP', '*', 'NINE', 'SIX', 'THREE', '.', 'PGDN', '-', '+', 'NULL13', 'NULLENTER', 'NULL14'
];





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];

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


}
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"
}