package controller

import (
	"net"
	. "netease_control_center/globals"
	"netease_control_center/interfaces/log"
	"netease_control_center/modules/player"
)

var (
	internetConnectFlag bool = false
)

func InternetConnectEvent() {
	log.Println(log.DebugLog, "Internet Connected Event!")
}

func InterneteDisconnectEvent() {
	log.Println(log.DebugLog, "Internet Disconnected Event!")
}

func NetworkServiceSwitch(on bool) {
	if on == true && BtNameGetFlag == true && internetConnectFlag == true {
		if !CurMusicSrcOnOffSt.DlnaOn {
			player.DLNAOn()
		}
		if !CurMusicSrcOnOffSt.AirPlayOn {
			player.CtrlAirplayOnOff(CMD_SPLAYER_START)
		}
	} else {
		if CurMusicSrcOnOffSt.DlnaOn {
			player.DLNAOff()
		}
		if CurMusicSrcOnOffSt.AirPlayOn {
			player.CtrlAirplayOnOff(CMD_SPLAYER_STOP)
		}
	}
}

func WifiConnectedEvent() {
	log.Println(log.DebugLog, "Wifi connected event!")

	TrigeNetworkCheck()
	internetConnectFlag = true
	NetworkServiceSwitch(internetConnectFlag)

	WifiIp, WifiMac = GetLocalIpMac()

	if len(WifiIp) > 0 {
		VboxIp = WifiIp
	}
	//// dlna go runtine
	//go func() {
	//	if !CurMusicSrcOnOffSt.DlnaOn {
	//		try := 0
	//		for {
	//		_dlnaOn:
	//			player.DLNAOn()
	//			select {
	//			case <-dlnaCreateChan:
	//				log.Printf(log.DebugLog, "dlna has create successfully.\n")
	//			case <-time.NewTimer(30 * time.Second).C:
	//				try++
	//				log.Printf(log.DebugLog, "wait dlna msg timeout, try:%d\n", try)
	//				goto _dlnaOn
	//			}
	//			return
	//		}
	//	}
	//}()
	//// airplay go runtine
	//go func() {
	//	if !CurMusicSrcOnOffSt.AirPlayOn {
	//		try := 0
	//		for {
	//		_airPlayON:
	//			player.CtrlAirplayOnOff(CMD_SPLAYER_START)
	//			select {
	//			case <-airPlayCreateChan:
	//				log.Printf(log.DebugLog, "airplay has create successfully.\n")
	//			case <-time.NewTimer(30 * time.Second).C:
	//				try++
	//				log.Printf(log.DebugLog, "wait airplay msg timeout, try:%d\n", try)
	//				goto _airPlayON
	//			}
	//			return
	//		}
	//	}
	//}()

	RestartZeroconf()
}

func WifiDisconnectedEvent() {
	log.Println(log.DebugLog, "Wifi disconnected event!")
	SetInternetConnect(false, "wifiEvent")
	internetConnectFlag = false
	NetworkServiceSwitch(internetConnectFlag)

	Iot_Wifiparing_disconnect()
}

func getLocalIp() *string {
	addrs, err := net.InterfaceAddrs()

	if err != nil {
		log.Println(log.ErrorLog, err.Error())
		return nil
	}

	for _, address := range addrs {
		// 检查ip地址判断是否回环地址
		if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
			if ipnet.IP.To4() != nil {
				ip := ipnet.IP.String()
				return &ip
			}
		}
	}
	return nil
}

func GetLocalIpMac() (ip, mac string) {
	inter, err := net.InterfaceByName("wlan0")
	if err != nil {
		log.Println(log.ErrorLog, "Get wlan0 err:", err)
		return
	}

	//mac地址
	mac = inter.HardwareAddr.String()
	addrs, err := inter.Addrs()
	if err != nil {
		log.Println(log.ErrorLog, "Get addr err:", err)
		return
	}
	for _, addr := range addrs {
		if tmp, ok := addr.(*net.IPNet); ok {
			if tmp.IP.To4() != nil {
				ip = tmp.IP.String()
			}
		}
	}

	return
}
