export function Name() { return "Valkyrie VK99"; } //名称
export function VendorId() { return 0x05AC; }
export function ProductId() { return 0x024F; }
export function Publisher() { return "Nollie & I'm Not MentaL"; } //发布者
export function Size() { return [103, 32]; }
export function Type() { return "Hid"; }
export function DefaultPosition(){return [10, 100]; }
export function DefaultScale(){return 8.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:		28/03/2024
Author: 	I'm Not MentaL & Nollie(Nuonuo)
Version:	V0.1
*/
const indexes = [0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x77,0x70,0x73,
	0x75,0x78,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F,0x67,
	0x74,0x20,0x21,0x22,0x7A,0x25,0x26,0x27,0x28,0x29,0x2A,0x2B,0x2C,0x2D,0x2E,0x2F,
	0x30,0x31,0x43,0x76,0x32,0x33,0x34,0x7B,0x37,0x38,0x39,0x3A,0x3B,0x3C,0x3D,0x3E,
	0x3F,0x40,0x41,0x42,0x55,0x79,0x44,0x45,0x46,0x49,0x4A,0x4B,0x4C,0x4D,0x4E,0x4F,
	0x50,0x51,0x52,0x53,0x54,0x65,0x56,0x57,0x58,0x6A,0x5B,0x5C,0x5D,0x5E,0x5F,0x60,
	0x62,0x63,0x64,0x66,0x68,0x69]
//高端的代码往往只需要最朴素的方式
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
];

const vKeyPositions =[
	[2, 0],         [12, 0], [17, 0], [22, 0], [27, 0], [34, 0], [39, 0], [44, 0], [49, 0], [56, 0], [61, 0], [66, 0], [71, 0], [79, 0], [85, 0], [90, 0], [95, 0], [100, 0], 
	[2, 7], [7, 7], [12, 7], [17, 7], [22, 7], [27, 7], [32, 7], [37, 7], [42, 7], [47, 7], [52, 7], [57, 7], [62, 7], [69, 7], [79, 7], [85, 7], [90, 7], [95, 7], [100, 7], 
	[3, 12],[9,12], [14,12], [19,12], [24,12], [29,12], [34,12], [39,12], [44,12], [49,12], [54,12], [59,12], [64,12], [70,12], [79,12], [85,12], [90,12], [95,12], [100,15],
	[4, 17],[10,17],[15,17], [20,17], [25,17], [30,17], [35,17], [40,17], [45,17], [50,17], [55,17], [60,17],       [68,17],    [79,17], [85,17], [90,17], [95,17], 
	[5, 22],[13,22],[18,22], [23,22], [28,22], [33,22], [38,22], [43,22], [48,22], [53,22], [58,22],               [67,22],     [79,23], [85,22], [90,22], [95,22], [100,25], 
	[3, 27],[9, 27],[15, 27], 					[34, 27], 						  [52,27],   [59, 27],    [65, 27],   [72, 28], [79, 28],[84,28], [90,27], [95,27]
];

const vKeyNames = 
[ 	"Esc",         "F1", "F2", "F3", "F4",   "F5", "F6", "F7", "F8",   "F9", "F10", "F11", "F12",    "Delete",		  "Prtsc",  "Pause",  "Home",  "End",     //14

	"`",     "1",  "2",  "3",  "4",  "5",  "6",  "7", "8", "9", "0", "-_", "=+","Backspace",          "Insert",        "NumLock","Num /","Num *", "Num -",   //19

	"Tab",       "Q",  "W",  "E",  "R",  "T", " Y", "U","I", "O", "P",  "[",  "]",   "\\",            "PgUp",         "Num 7", "Num 8", "Num 9", "Num +",   //19

	"CapsLock",       "A",  "S",  "D",  "F", "G", "H", "J", "K",  "L", ";", "'",    "Enter",          "PgDn",        "Num 4", "Num 5", "Num 6",            //17

	"Left Shift",      "Z", "X", "C", "V", "B", "N", "M", ",", ".", "/",          "Right Shift",    "Up Arrow",       "Num 1", "Num 2", "Num 3","Num Enter",  //17

	"Left Ctrl","Left Win","Left Alt",      "Space",     "Right Alt","Fn","Right Ctrl","Left Arrow","Down Arrow","Right Arrow","Num 0","Num ." //12
 ];

function refresh()
{
	let packet = new Array(65).fill(0);
	device.send_report(packet, 65);
	device.pause(1);
	packet[1] = 0x04;
	packet[2] = 0x02;
	device.send_report(packet, 65);
	device.pause(1);
	let data = new Array(65).fill(0);
	device.get_report(data,65);
	device.pause(1);

}
function start_refresh()
{
	let packet = new Array(65).fill(0);
	packet[1] = 0x04;
	packet[2] = 0x20;
	packet[9] = 0x08;
	device.send_report(packet, 65);
	device.pause(1);
	let data = new Array(65).fill(0);
	device.get_report(data,65);
	device.pause(1);
	
	// device.send_report(data,1);
}




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() {

}



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 send_data = [];

	// packet = packet.concat(rgbdata);	
	for(var index = 0; index < 102; index++) //This will need rounded up to closest value for your board.
	{
		send_data.push(indexes[index],rgbdata[index * 3],rgbdata[index* 3 +1],rgbdata[index * 3 + 2])

	}	
	start_refresh();
	for(var index = 0; index <= 6; index++)
	{
		let packet = [0x00];
		packet.push(...send_data.splice(0, 64));
		device.send_report(packet, 65);
		device.pause(1);
	}	
	refresh();
	
}
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 === 0x0006 && endpoint.usage_page === 0x0001;
	return endpoint.interface === 2 && endpoint.usage === 0x0001 && endpoint.usage_page === 0xff13;
}

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