package main

import (
	"fmt"
	"io"
	"log"
	"net/http"

	"github.com/beanzilla/glom"
	"github.com/beego/x2j"
)

// https://zetcode.com/golang/getpostrequest/ post_req_form.go
func Post(where string) *http.Response {
	resp, err := http.PostForm(where, nil)
	if err != nil {
		log.Printf("POST got non 200 code, %v", err)
	}
	return resp
}

func Get(where string) *http.Response {
	resp, err := http.Get(where)
	if err != nil {
		log.Printf("GET got non 200 code, %v", err)
		return nil
	} else {
		return resp
	}
}

// https://developer.roku.com/docs/developer-program/debugging/external-control-api.md#keypress-key-values codes
func KeyPress(ip, command string) string {
	return fmt.Sprintf("http://%s:8060/keypress/%s", ip, command)
}

// https://developer.roku.com/docs/developer-program/debugging/external-control-api.md#general-ecp-commands
func Query(ip, what string) string {
	return fmt.Sprintf("http://%s:8060/query/%s", ip, what)
}

func PerformKey(ip, command string) {
	Post(KeyPress(ip, command))
}

func GetCurrentPlay(ip string) map[string]interface{} {
	resp := Get(Query(ip, "media-player"))
	if resp == nil {
		return nil
	}
	body, err := io.ReadAll(resp.Body)
	if err != nil {
		log.Printf("Reading from body got error, %v", err)
	}
	r := make(map[string]interface{})
	err = x2j.Unmarshal(body, &r)
	if err != nil {
		log.Printf("Got a error parsing XML, %v", err)
	}
	return r
}

func GetQuery(ip, command string) map[string]interface{} {
	resp := Get(Query(ip, command))
	if resp == nil {
		return nil
	}
	body, err := io.ReadAll(resp.Body)
	if err != nil {
		log.Printf("Reading from body got error, %v", err)
	}
	r := make(map[string]interface{})
	err = x2j.Unmarshal(body, &r)
	if err != nil {
		log.Printf("Got a error parsing XML, %v", err)
	}
	return r
}

func DebugInfo(ip string) string {
	// Query the current playing thing, and get the tv channels
	r := GetQuery(ip, "media-player")
	if r == nil {
		return ""
	}
	name, _ := glom.Glom(r, "player.plugin.-name")
	id, _ := glom.Glom(r, "player.plugin.-id")
	/*r1 := GetQuery(ip, "tv-channels")
	chan_name, _ := glom.Glom(r1, "tv-channels.channel.name")
	chan_phy_id, _ := glom.Glom(r1, "tv-channels.channel.physical-channel")
	chan_number, _ := glom.Glom(r1, "tv-channels.channel.number")*/

	// Debug print
	fmt.Printf("Name: %v (%v)\r\n", name, id)
	//fmt.Printf("Channel: %v (%v / %v)\r\n", chan_name, chan_phy_id, chan_number)
	return fmt.Sprintf("%v (%v)", name, id)
}

func examplePost() {
	keys := GetKeys()
	_ = keys // Keep alive
	PerformKey("192.168.254.75", keys["mute"])
	//Post("http://192.168.254.75:8060/launch/74519")
	//PerformKey("192.168.254.75", keys["home"])

	/*
		//resp := Post("http://192.168.254.75:8060/keypress/volumeMute")
		//resp := Post(KeyPress("192.168.254.75", "volumeMute"))
		//resp := Get(Query("192.168.254.75", "device-info"))
		//resp := GetQuery("192.168.254.75", "tv-active-channel")
		resp := GetQuery("192.168.254.75", "tv-channels")

		//result, err := glom.Glom(resp, "tv-channels.channel")
		_, err := glom.Glom(resp, "*")
		if err != nil {
			log.Println(err)
		} else {
			//fmt.Println(result)
			fmt.Println(glom.GetPossible(result))
		}
	*/

	// Query the current playing thing, and get the tv channels
	r := GetQuery("192.168.254.75", "media-player")
	name, _ := glom.Glom(r, "player.plugin.-name")
	id, _ := glom.Glom(r, "player.plugin.-id")
	r1 := GetQuery("192.168.254.75", "tv-channels")
	chan_name, _ := glom.Glom(r1, "tv-channels.channel.name")
	chan_phy_id, _ := glom.Glom(r1, "tv-channels.channel.physical-channel")
	chan_number, _ := glom.Glom(r1, "tv-channels.channel.number")

	// Debug print
	fmt.Printf("Name: %v (%v)\r\n", name, id)
	fmt.Printf("Channel: %v (%v / %v)\r\n", chan_name, chan_phy_id, chan_number)
}
