using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace JellyfinRPC
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox1 form = new AboutBox1();
            form.Show();
        }

        private void quitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void serverConfigToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ConfigForm form = new ConfigForm();
            form.Show();
        }

        private static System.Timers.Timer consoleTimer;

        private void Form1_Load(object sender, EventArgs e)
        {
            minimiseToTrayToolStripMenuItem.Checked = bool.Parse(ConfigManager.GetEntry("MinimiseToTray", "False"));
            closeToTrayToolStripMenuItem.Checked = bool.Parse(ConfigManager.GetEntry("CloseToTray", "False"));
            ConfigManager.GetEntry("ApiKey", "");
            ConfigManager.GetEntry("ServerURL", "");
            ConfigManager.GetEntry("UserID", "");
            ConfigManager.GetEntry("JellyfinToken", "");
            openToTrayToolStripMenuItem.Checked = bool.Parse(ConfigManager.GetEntry("OpenToTray", "False"));
            ConfigManager.GetEntry("JellyfinToken", "");
            ConfigManager.GetEntry("DeviceID", $"{JellyfinAPI.DeviceID()}");
            SetTimer();
            JellyfinAPI.Jellyfin();
            
            
                

                //if (ConfigManager.GetEntry("JellyfinToken") == "" && ConfigManager.GetEntry("ApiKey") == "")
                //{
                //    ConfigForm form = new ConfigForm();
                //    form.Show();
                //}

            }

            private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Form1 form = new Form1();
            form.Show();
        }

        public void SetTimer()
        {
            consoleTimer = new System.Timers.Timer(5);
            consoleTimer.Elapsed += ConsoleTimer_Elapsed;
            consoleTimer.AutoReset = true;
            consoleTimer.Enabled = true;
            
        }

        private void ConsoleTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {



                string last = listBox1.Items[listBox1.Items.Count - 1].ToString();
                
                if (last == ConsoleManager.lineToWrite)
                {
                    return;
                }
                else
                {
                    listBox1.BeginInvoke((MethodInvoker)delegate
                    {
                        if (ConsoleManager.lineToWrite != null)
                        {
                            listBox1.Items.Add(ConsoleManager.lineToWrite);
                        }
                        
                        
                    });
                }
                
                

            }
            catch (NullReferenceException)
            {
                return;
            }
            
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            if (ConfigManager.GetEntry("MinimiseToTray") == "True")
            {
                if (this.WindowState == FormWindowState.Minimized)
                {
                    Hide();
                    notifyIcon1.Visible = true;
                    notifyIcon1.ShowBalloonTip(5000);
                }
            }
        }

        private void minimiseToTrayToolStripMenuItem_Click(object sender, EventArgs e)
        {
           ConfigManager.SetEntry("MinimiseToTray", minimiseToTrayToolStripMenuItem.Checked.ToString());
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void showToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Show();
            notifyIcon1.Visible = false;
        }

        private void notifyIcon1_Click(object sender, EventArgs e)
        {
            Show();
        }

        private void quitToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void openToTrayToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ConfigManager.SetEntry("OpenToTray", openToTrayToolStripMenuItem.Checked.ToString());
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (ConfigManager.GetEntry("CloseToTray") == "True")
            {
                if (e.CloseReason == CloseReason.UserClosing)
                {
                    e.Cancel = true;
                    Hide();
                    notifyIcon1.Visible = true;
                    notifyIcon1.ShowBalloonTip(5000);
                    
                }
            }
            else
            {
                Application.Exit();
            }
        }

        private void closeToTrayToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ConfigManager.SetEntry("CloseToTray", closeToTrayToolStripMenuItem.Checked.ToString());
        }

        private void rPCSettingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form3 form = new Form3();
            form.Show();
        }

        private void testConsoleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ConsoleManager.WriteToConsole("Testing Console...");
        }

        private void writeLabelToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StatusManager.WriteStatusLine("Testing Status...");
        }


        private void toolStripStatusLabel1_Click(object sender, EventArgs e)
        {

        }
    }
}
