export function Name() { return "佳宫75Max"; } //名称
export function VendorId() { return 0x320f; }
export function ProductId() { return 0x5084; }
export function Publisher() { return "随机复读的复读姬"; } //发布者
export function Size() { return [16, 6]; }
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:2023/7/7
Author: 随机复读的复读姬
Version:V0.1
*/
const vKeys = 
[
    0,  1,  2,  3,  4,  5,  6,  7,  8,   9, 10, 11, 12,  13,    //F区 
	16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, //数字区
	32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, //qwer
	48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 61,     31, //asdf
	64,     66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 77, 78, 47, //zxcv
	80, 81, 82,             85,            89,90, 92,93,94, 95, //ctrl
];
const vKeyPositions = 
[
[0, 0], [1, 0], [2, 0], [3, 0], [4, 0], [5, 0], [6, 0], [7, 0], [8, 0], [9, 0], [10, 0], [11, 0], [12, 0],          [14, 0], 
[0, 1], [1, 1], [2, 1], [3, 1], [4, 1], [5, 1], [6, 1], [7, 1], [8, 1], [9, 1], [10, 1], [11, 1], [12, 1],          [14, 1], [15, 1],
[0, 2], [1, 2], [2, 2], [3, 2], [4, 2], [5, 2], [6, 2], [7, 2], [8, 2], [9, 2], [10, 2], [11, 2], [12, 2], [13, 2],          [15, 2],
[0, 3], [1, 3], [2, 3], [3, 3], [4, 3], [5, 3], [6, 3], [7, 3], [8, 3], [9, 3], [10, 3], [11, 3], [12, 3],                   [15, 3], //14
[0, 4],         [2, 4], [3, 4], [4, 4], [5, 4], [6, 4], [7, 4], [8, 4], [9, 4], [10, 4], [11, 4], [12, 4],          [14, 4], [15, 4], //14
[0, 5], [1, 5], [2, 5],                 [5, 5],                                 [10, 5], [11, 5], [12, 5], [13, 5], [14, 5], [15, 5]  //14  
];


const vKeyNames = 
[
    "Esc","F1","F2","F3","F4","F5","F6","F7","F8","F9","F10","F11","F12","Del",                                    //13
    "`","1","2", "3", "4", "5",  "6", "7", "8", "9", "0",  "-", "+",  "Backspace","Home",                          //13
    "Tab", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "[", "]", "\\","Page Up",                             //15
    "CapsLock", "A", "S", "D", "F", "G", "H", "J", "K", "L", ";", "'","Enter","Page Down",                         //14
    "Left Shift","Z", "X", "C", "V", "B", "N", "M", ",", ".", "/", "Right Shift","Up Arrow",   "End",              //14
    "Left Ctrl","Left Win","Left Alt","Space","Right Alt","Fn","Right Ctrl","Left Arrow","Down Arrow","Right Arrow"//14
];


export function LedNames() {
	return vKeyNames;

}

export function LedPositions() {
	return vKeyPositions;
}

const MaxLedsInPacket = 56;
export function Render() {
	sendColors();
}

export function Shutdown() {

}

function sumArray(array) {
	const sum = array.filter(Number.isFinite) 
				   .reduce((acc, num) => acc + num, 0); 
	return sum;
}

function getHighLow(num) {
	const high = (num >>> 8) & 0xFF; 
	const low = num & 0xFF; 
	return { high, low }; 
  }

function sendColors(shutdown = false)
{

	let RGBData = grabColors();
	for(var index = 0; index < 7; index++) 
  {
		let packet = [];
		let rgbPeriod = [];
		rgbPeriod.push(...RGBData.splice(0, MaxLedsInPacket));
		if (index >=5)
		{
			var  RGB_Total = sumArray(rgbPeriod)+((index-5)* MaxLedsInPacket)+ 0x63;
		} 
		else
		{
			var  RGB_Total = sumArray(rgbPeriod)+(index* MaxLedsInPacket)+ 0x4A;
		}	
		packet[0] = 0x04;
		packet[1] = getHighLow(RGB_Total).low;
		packet[2] = getHighLow(RGB_Total).high;
		packet[3] = 0x12;
		packet[4] = 0x38;
		if (index == 6)
		{
			packet[4] = 0x30;
		}
		packet[5] = getHighLow(index * MaxLedsInPacket).low;
		packet[6] = getHighLow(index * MaxLedsInPacket).high;
		packet[7] = 0x00;
		packet = packet.concat(rgbPeriod);
		device.write(packet, 64);
	    }
}



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 === 0x0092 && endpoint.usage_page === 0xff1c;
}

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