using OverlayFormObj;
using DBObj;
using MySqlConnector;
using System.Data;

namespace DualScreenDemo
{
    public class Room
    {
        public int branch_id =0;
        public string hostName;
        
        private string State = "error";
        public Room()
        {
            hostName = System.Net.Dns.GetHostName();
            branch_id =getBranchId();
            State =getDB();
            Console.WriteLine($"hostname status: {hostName},{State}");
        }
        private int getBranchId()
        {
            using (var db = new MyDB())
            {
                string sql = "SELECT id FROM branches LIMIT 1";
                if (db.open(sql, null) && db.found())return  db.Field<int>("id");
                return 0;
            }
        }
        
        private string getDB()
        {
            using (var db = new MyDB())
            {
                string query = @"SELECT * FROM rooms WHERE branch_id = @branch_id AND CONCAT(type, name) = @hostName";
                MySqlParameter[] parameters = {
                    new MySqlParameter("@branch_id", branch_id),
                    new MySqlParameter("@hostName", hostName)
                };
                if (db.open(query, parameters) && db.found())
                {
                    State = db.Field<string>("status");
                    return State;
                }
            }
            return "error";
        }
        private void setDB(string value)
        {
            using (var db = new MyDB())
            {
                string query = @"UPDATE  rooms SET status = @status WHERE branch_id = @branch_id AND CONCAT(type, name) = @hostName;";
                MySqlParameter[] parameters = {
                    new MySqlParameter("@status", value),
                    new MySqlParameter("@branch_id", branch_id),
                    new MySqlParameter("@hostName", hostName)
                };
                db.ExecuteNonQuery(query, parameters);
            }
        }
        public void set(string value)
        {
            string marqueeMessage = "歡迎使用超級巨星歡唱，與你共度美好時光。";
            Color c = Color.White;
            setDB(value);
            if (value.Equals("fire"))
            {
                PrimaryForm.Instance.ShowSendOffScreen();
                VideoPlayerForm.Instance.Pause();
                marqueeMessage = "發生火災，請跟隨引導至逃生出口!!!";
                c = Color.Red;
            }
            else if (value.Equals("active"))
            {
                DBObj.SongList.clearSong();
                PrimaryForm.Instance.HotPlayButton_Click(null, EventArgs.Empty);
                PrimaryForm.Instance.HideSendOffScreen();
            }
            else
            {
                DBObj.SongList.clearSong();
                PrimaryForm.Instance.pictureBoxQRCode.Visible = false;
                PrimaryForm.Instance.closeQRCodeButton.Visible = false;
                PrimaryForm.Instance.ShowSendOffScreen();


                OverlayForm.MainForm.topLeftLabel.Visible = false;

                VideoPlayerForm.Instance.PlayNextSong();
                PrimaryForm.Instance.logout();

            }
            State =getDB();
            OverlayForm.MainForm.UpdateMarqueeText(marqueeMessage, OverlayForm.MarqueeStartPosition.Middle, c);

        }
        public bool IsClose()
        {
            return State.Equals("closed");
        }
        public bool IsOpen()
        {
            return State.Equals("active");
        }
        
    }
    
    
    
    
    
}