#!/usr/bin/perl
use warnings;
use strict;

use Term::ReadKey;
use Data::Dump qw(dump);
use Time::HiRes qw(sleep);

my $name2ir;

while(<DATA>) {
	chomp;
	my ($code,$name) = split(/\t+/,$_,2);
	$name2ir->{lc($name)} = $code;
}
warn "# name2ir = ",dump($name2ir);

my $direction = {
	A => 'UP',
	B => 'DOWN',
	C => 'RIGHT',
	D => 'LEFT',
};

sub ir {
	my $key = shift;
	$key = lc $key;
	if ( my $data = $name2ir->{$key} ) {
		my $cmd = qq{mosquitto_pub -h rpi2 -q 2 -t cmnd/ir/IRSend -m '{"protocol": "NEC","bits": 32, "data": $data}'};
		warn "# [$key] $cmd\n";
		system($cmd);
	} else {
		warn "ERROR: missing mapping for [$key]";
	}
}

ReadMode 4; # Turn off controls keys
my $key;
my $esc;
while(1) {
	while (not defined ($key = ReadKey(1))) {
		# No key yet
	}
	print dump($key, $esc), $/;
	if ( $key eq 'q' ) {
		ReadMode 0; # Reset tty mode before exiting
		exit 0;
	}
	if ( $key eq "\e" ) {
		$esc = $key;
		$key = '?';
	} elsif ( $esc ) {
		$esc .= $key;
	}

	if ( $esc && $esc =~ /\e\[([ABCD])/ ) {
		printf "arrow: %s %s\n", $1, $direction->{$1};
		ir($direction->{$1});
		$esc = '';
	} elsif ( $key eq "\e" && $esc eq "\e" ) { # single ESC
		ir('BACK');
		$esc = '';
	} else {
		warn "UNKNOWN esc,key = ",dump($esc, $key);
	}

	ir('MENU') if $key eq 'm';
	ir('INFO') if $key eq 'i';
	ir('on/off') if $key eq 'p';
	ir('settings') if $key eq 's';
	ir('OK') if $key eq 'o' || $key eq "\n" || $key eq " ";

	ir('EXIT') if $key eq "\x7F"; # backspace

	ir('input') if $key eq 'w';
	ir('energy') if $key eq 'e';

	ir('Q.View') if $key eq 'a';
	# s
	ir('guide') if $key eq 'd';

	ir('HDMI 1') if $key eq 'z';
	ir('HDMI 2') if $key eq 'x';
	ir('HDMI 3') if $key eq 'c';

}

__DATA__
0x20DF10EF	ON/OFF
0x20DF0FF0	TV
0x20DF9E61	Ratio
0x20DFD02F	Input
0x20DFA956	Energy
0x20DF8877	Key 1
0x20DF48B7	Key 2
0x20DFC837	Key 3
0x20DF28D7	Key 4
0x20DFA857	Key 5
0x20DF6897	Key 6
0x20DFE817	Key 7
0x20DF18E7	Key 8
0x20DF9867	Key 9
0x20DFCA35	List
0x20DF08F7	Key 0
0x20DF58A7	Q.View
0x20DF40BF	Vol+
0x20DFC03F	Vol-
0x20DFCE31	Vol-
0x20DF7887	Fav
0x20DF3BC4	3D
0x20DF906F	Mute
0x20DF00FF	P+
0x20DF807F	P-
0x20DFC23D	Settings
0x20DF55AA	Info
0x20DFA25D	Menu
0x20DFE01F	Left
0x20DF609F	Right
0x20DF02FD	Up
0x20DF827D	Down
0x20DF22DD	OK
0x20DF14EB	Back
0x20DFD52A	Guide
0x20DFDA25	Exit
0x20DF4EB1	Red
0x20DF8E71	Green
0x20DFC639	Yellow
0x20DF8679	Blue
0x20DF04FB	Text
0x20DF847B	T.Opt
0x20DF9C63	Subtitle
0x20DF8D72	Stop
0x20DF0DF2	Play
0x20DF5DA2	Pause
0x20DFF10E	Backward
0x20DF718E	Forward
0x20DF7E81	Simplink
0x20DF8976	AD
0x20DF0CF3	AV Mode
0x20DF3EC1	Live TV
0x20DF42BD	Live TV
0x20DF35CA	Status
0x20DF50AF	Audio Language
0x20DF57A8	Shortmenu
0x20DF5AA5	AV
0x20DF5EA1	online manual
0x20DF619E	Volume to 50
0x20DF21DE	Volume to 80
0x20DFE11E	Volume to 100
0x20DF6B94	Radio/TV
0x20DF708F	doze function
0x20DFA35C	Display off
0x20DFD728	Standby
0x20DF23DC	Power on
0x20DFB44B	Settings of remote
0x20DF956A	Program guide
0x20DFF00F	Radio/TV
0x20DF738C	HDMI 1
0x20DF33CC	HDMI 2
0x20DF9768	HDMI 3
0x20DFFD02	Component
0x20DFF50A	Live Zoom
0x20DFF20D	Input info
0x20DFB24D	Picture mode
0x20DF09F6	Recordings
0x20DF4AB5	Audio mode
0x20DF7B84	Register magic remote
0x20DFAE51	directly register magic remote
0x20DF1CE3	Subtitles
0x20DFCC33	Presentation in shop
0x20DFBD42	Start recording
0x20DFAD52	LG TV Plus
0x20DFDD22	Sports mode
0x20DF1EE1	Search
0x20DFDF20	Warning: instart
0x20DF7F80	Warning: Poweronly
0x20DFFF00	Warning: Easy adjust
0x20DF5FA0	Warning: instop
0x20DF5FA0	Enter Password
0x20DF3FC0	Warning: Reset to factory
