using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using BroadcastModule;

namespace parkMonitor.server.broadcastModule
{
    public class BroadcastLinker
    {
        public static BroadcastBoard ins;
        static string ip;
        static int port;

        /// <summary>
        /// 初始化单例
        /// </summary>
        public static void Start()
        {
            Task.Factory.StartNew(() =>
            {
                try
                {
                    ip = ConfigurationManager.AppSettings["broadcastIP"];
                    port = Int32.Parse(ConfigurationManager.AppSettings["broadcastPort"]);
                    ins = new BroadcastBoard(ip, port);
                    ins.Refresh();
                    ins.Play(0, BroadcastBoard.PlayMode.audio, "欢迎光临");
                    //OriginState();
                }
                catch (Exception e) { Console.WriteLine(e.Message); ins = null; }
            });
        }

        public static void dispForAWhile(int winID, string str, int delay, bool switchToOrigin)
        {
            if (ins != null)
            {
                ins.Play(winID, BroadcastBoard.PlayMode.temporary, str, 5);
                Task.Factory.StartNew(() =>
                {
                    if (switchToOrigin)
                    {
                        Thread.Sleep(delay * 1000);
                        OriginState();
                    }
                });
            }
        }
        public static void OriginState()
        {
            if (ins != null)
            {
                ins.Play(0, BroadcastBoard.PlayMode.readBuffer, "", 0, 0);
                ins.Play(1, BroadcastBoard.PlayMode.readBuffer, "", 0, 0);
                ins.Play(2, BroadcastBoard.PlayMode.readBuffer, "", 0, 0);
                ins.Play(3, BroadcastBoard.PlayMode.readBuffer, "", 0, 0);
            }
        }
    }
}
