using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Threading;
using MiniBar2k.Common;

namespace MiniBar2k.PC
{
    public partial class Form1 : Form
    {
        private FoobarController fbc = null;
        private FoobarPlaylist fbp = null;
        private FoobarUI fbui = null;

        public Form1()
        {
            InitializeComponent();
        }

        private void OnNewLine(object sender, String line)
        {
            listBox1.Items.Add(line);
            listBox1.SelectedIndex = listBox1.Items.Count - 1;
        }

        private void OnConnectionStateChange(object sender, fbcThreadState state)
        {
            switch (state)
            {
                case fbcThreadState.connected: Text = "Connected"; break;
                case fbcThreadState.connecting: Text = "Connecting"; break;
                case fbcThreadState.disconnected: Text = "Disconnected"; break;
                case fbcThreadState.disconnecting: Text = "Disconnecting"; break;
            }
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            if (fbc == null)
            {
                fbc = new FoobarController("127.0.0.1", 3333);
                fbc.OnNewLine += OnNewLine;
                fbc.OnConnectionStateChange += OnConnectionStateChange;

                fbui = new FoobarUI(fbc, dataGrid1, mainMenu1, btnStop, btnPause, btnPlay, btnPrev, btnNext, btnRand);

                fbp = new FoobarPlaylist(fbc);
                fbc.Connect();
                fbp.Become(0);
            }
            else
            {
                fbc.Resync();
            }
            dataGrid1.DataSource = fbp;
            dataGrid1.TableStyles[0].GridColumnStyles[0].Width = dataGrid1.Width - 20;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (fbc != null) { fbc.Tick(); }
        }

        private void btnDisconnect_Click(object sender, EventArgs e)
        {
            if (fbc != null) { fbc.Disconnect(); }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (fbc != null) { fbc.Dispose(); }
        }

        private void listBox1_Format(object sender, ListControlConvertEventArgs e)
        {
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
        }

        private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
        }

        private void menuItem3_Click(object sender, EventArgs e)
        {

        }

        private void dataGridView1_Click(object sender, EventArgs e)
        {

        }

        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            Text = fbc.FormatTitle(fbp.lastplaylist, fbp.lasttrack, false);
        }

        private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            //dataGrid1.DataSource = null;
            dataGrid1.CurrentRowIndex = 500;
        }
    }
}