package test

import (
	"bufio"
	. "netease_control_center/globals"
	"netease_control_center/interfaces/log"
	"os"
	"strings"
)

const (
	CMD_HELP = iota
)

const (
	CMD_PLAYER_AUDIO_MP3_URL = 0X750 + iota
	CMD_PLAYER_AUDIO_PCM_TEXT

	CMD_PLAYER_NULL = 0Xffff
)

type Command struct {
	strCommand string
	nCommand   uint32
	strHelpMsg string
}

var constCommandArr []Command = []Command{
	{"help", CMD_HELP, "show this help message"},
	{"play", CMD_PLAYER_PLAY, "start playback."},
	{"pause", CMD_PLAYER_PAUSE, "pause the playback."},
	{"stop", CMD_PLAYER_STOP, "stop the playback."},
	{"seek to", CMD_PLAYER_SEEKTO, "seek to specific position to play, position is in unit of second, for example, seek to: 100."},
	{"next", CMD_PLAYER_NEXT, "next music."},
	{"pre", CMD_PLAYER_PRE, "pre music."},

	{"pre next", uint32(MUSIC_ST_FOR_NEXT), "prepare for next."},

	{"list", CMD_SHOW_LIST, "show list"},
	{"his", CMD_SHOW_HIS, "show his"},
	{"re tts", CMD_REPLAY_TTS, "replay tts test"},
	{"daily", CMD_DAILY_PUSH, "get daily src play"},
	{"new", CMD_NEW_TOP, "get new src and play"},
	{"hot", CMD_HOT_TOP, "get hot src and play"},
	{"denon", CMD_DENON_TOP, "get denon src and play"},
	{"classic", CMD_CLASSIC_TOP, "get classic src and play"},
	{"beaport", CMD_BEAPORT_TOP, "get beaport src and play"},
	{"original", CMD_ORIGINAL_TOP, "get original src and play"},
	{"sorign", CMD_SOARING_TOP, "get sorign src and play"},
	{"acg", CMD_ACG_TOP, "get ACG src and play"},
	{"ch new", CMD_CH_NEW, "get ch new src and play"},
	{"jp new", CMD_JP_NEW, "get jp new src and play"},
	{"clist", CMD_CREALTE_LIST, "get create list and play"},
	{"mode", CMD_MODE_CHANGE, "play mode change, 0:list , 1:rand , 2: single"},
	{"fm", CMD_PRIVATE_FM, "get private fm and play"},
	{"ttsl", CMD_TTS_LEVEL, "tts level test"},
	{"alarms", CMD_ALARM_S, "alarm short voic"},
	{"alarml", CMD_ALARM_L, "alarm long voice"},
	{"otaTts", CMD_OTA_TTS, "test ota tts"},
	{"se", CMD_SE_TEST, "sound effect test, se:0 fail se  se:1 success se"},
	{"dlna", CMD_DLNA_CTRL, "ctrl dlna, dlna:0 ->close, dlna:1 ->open"},
	{"dlnaS", CMD_CTRL_DLNA_ON_APP, "test vbox ctrl phone, 0:stop, 1:play 2:pause"},
	{"agency", CMD_AGENCY_TEST, "agency test list"},
	{"server", CMD_SERVER_CHANGE, "server change, server:1, online    server:0 test"},
	{"adurl", CMD_AUDIO_URL, "audio url type play, adurl:0 normal, adurl:1 mix "},
	{"aplay", CMD_AIRPLAY_CTRL, "airplay on/off, airplay:0 off , airplay:1 on"},
	{"ttsSel", CMD_TTS_SEL, "tts sel, ttsSel:0->讯飞; ttsSel:1->小雅; ttsSel:2->合成"},
	{"ttsLook", CMD_TTS_LOOK, "print ttsSel"},
	{"app1", APP_CMD_SINGLE, "app cmd of single"},
	{"app2", APP_CMD_CREATE_LIST, "app cmd of create list"},
	{"app3", APP_CMD_NEW, "app cmd of new toplist"},
	{"app4", APP_CMD_WORD, "app cmd of word"},
	{"app5", APP_CMD_LISTID, "app cmd of listid"},
	{"birthday", CMD_BIRTHDAY_TEST, "birthday info play test"},
	{"uuid", CMD_SET_UUUI, "set uuid by uer"},
	{"key", CMD_SET_KEY, "set key by uer"},
	{"token", CMD_SET_TOKEN, "set token by uer"},
	{"binduser", CMD_SET_BINDUSER, "set binduser to user"},
	{"bir", CMD_SET_BIRTHDAY, "set birthday to user"},
	{"bt", CMD_BT_RESET, "reset bt"},
	{"scene", CMD_SCENE_TEST, "scene test"},
	{"filterId", CMD_SONGID_SHIELD, "add songid to filter"},
	{"filterTts", CMD_FILTER_TTS, "filter song tts"},
	{"islike", CMD_LIKED_SEARCH, "is cur music liked?"},
	{"play url", CMD_PLAY_URL, "play music by URL, play url:$url"},
	{"bg", CMD_BGPLAYER_TEST, "back ground player test"},
	{"itop3", CMD_TOP3_INFORM_TEST, "inform top3 msg"},
	{"top3", CMD_TOP3_INSERT_TEST, "insert top3 songs"},
	{"vol", CMD_VOL_SET, "set vol, vol:10"},
	{"inscene", CMD_ENTER_SCENE, "enter scene, scene:id"},
	{"cfgList", CMD_PULL_CFG_LIST, "cfgList"},
	{"keyVol", CMD_KEY_VOL_TEST, "test key of vol, keyVol:0 close, keyVol:1 open"},
	{"ledC", CMD_DIRECT_LED_CLEAR_TEST, "clear direct led, ledC:1, start;  ledC:2 end"},
	{"replay", CMD_REPLAY_CUR, "replay cur misic"},
	{"rm", CMD_DIS_DEVICE, "disconnect bt/airplay/dlna test, rm:0 bt, rm:1 airplay rm:2 dlna"},
	{"initPlayer", CMD_INIT_PLAYER, "init player"},
	{"fcfg", CMD_FORCE_CFGCB, "force call cfglist cb"},
	{"scfg", CMD_CFG_SAVE_TEST, "cfglist save db test"},
	{"", 0, ""},
}

func ShowHelp() {
	for i := 0; ; i++ {
		if "" == constCommandArr[i].strCommand {
			break
		}
		log.Println(log.DebugLog, constCommandArr[i].strCommand+":")
		log.Printf(log.DebugLog, "\t\t%s\n", constCommandArr[i].strHelpMsg)
	}
}

func readCmd() (uint32, *string, error) {
	reader := bufio.NewReader(os.Stdin)
	log.Printf(log.DebugLog, "Input your cmd:")
	data, _, _ := reader.ReadLine()
	log.Printf(log.DebugLog, "Your input is:%s\r\n", data)
	cmd, params := praseCmd(data)
	return cmd, params, nil
}

func praseCmd(cmdByte []byte) (uint32, *string) {
	cmdStr := string(cmdByte)
	if nil == cmdByte {
		return CMD_PLAYER_NULL, nil
	}
	cmdArr := strings.Split(cmdStr, ":")
	for i := 0; ; i++ {
		if "" == constCommandArr[i].strCommand {
			break
		}
		if constCommandArr[i].strCommand == cmdArr[0] {
			var params string = ""
			if len(cmdArr) > 1 {
				for i := 1; i < len(cmdArr); i++ {
					params += cmdArr[i]
					if i != len(cmdArr)-1 {
						params += ":"
					}
				}
			}
			return constCommandArr[i].nCommand, &params
		}
	}
	return CMD_PLAYER_NULL, nil
}

func ReadInputCmd() (uint32, *string, error) {
	cmd, params, err := readCmd()
	return cmd, params, err
}
