using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using PCComm;
namespace LedCubeController
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        CommunicationManager comm = new CommunicationManager();
        bool isConnectionEstablished = false;

        public MainWindow()
        {
            InitializeComponent();
            comm.SetPortNameValues(portComboBox);
            //set default values
            comm.Parity = "None";
            comm.StopBits = "One";
            comm.DataBits = "8";
            comm.BaudRate = "3125000";
            comm.CurrentTransmissionType = PCComm.CommunicationManager.TransmissionType.Hex;
        }

        private void connectClick(object sender, RoutedEventArgs e)
        {
            comm.PortName = portComboBox.Text;
            isConnectionEstablished = comm.OpenPort();
            if (isConnectionEstablished){
                connectionStatusLabel.Content = "Connected";
                connectionStatusLabel.Foreground = Brushes.Green;
                this.changeMode();
            }else{
                
            }
        }

        private void sliderValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            try
            {
                colorCanvas.Background = new SolidColorBrush(
                Color.FromRgb(Convert.ToByte(redSlider.Value * 4),
                Convert.ToByte(greenSlider.Value * 4), Convert.ToByte(blueSlider.Value * 4)));
            }
            catch (OverflowException ex)
            {

            }
            
        }

        private void changeMode()
        {
            textToDisplayLabel.IsEnabled = (bool)textModeRB.IsChecked;
            textToDisplayTB.IsEnabled = (bool)textModeRB.IsChecked;
            redSlider.IsEnabled = (bool)textModeRB.IsChecked;
            greenSlider.IsEnabled = (bool)textModeRB.IsChecked;
            blueSlider.IsEnabled = (bool)textModeRB.IsChecked;
            timeIntervalCB.IsEnabled = (bool)textModeRB.IsChecked;
            displayTextButton.IsEnabled = (bool)textModeRB.IsChecked && isConnectionEstablished;
            rLabel.IsEnabled = (bool)textModeRB.IsChecked;
            bLabel.IsEnabled = (bool)textModeRB.IsChecked;
            gLabel.IsEnabled = (bool)textModeRB.IsChecked;
            changeIntervalLabel.IsEnabled = (bool)textModeRB.IsChecked;
            colorCanvas.IsEnabled = (bool)textModeRB.IsChecked;

            anim1.IsEnabled = !(bool)textModeRB.IsChecked && isConnectionEstablished;
            anim2.IsEnabled = !(bool)textModeRB.IsChecked && isConnectionEstablished;
            anim3.IsEnabled = !(bool)textModeRB.IsChecked && isConnectionEstablished;
        }

        private bool checkText(string text)
        {
            foreach (char c in text)
            {
                if ((c < 'A' || c > 'Z') && c != ' ')
                {
                    return false;
                }
            }
            return true;
        }

        private void displayText(object sender, RoutedEventArgs e)
        {

            if (!checkText(textToDisplayTB.Text))
            {
                MessageBox.Show("Only 'A'..'Z' and ' ' are permitted letters!");
                return;
            }

            string textCode = "C0";
            textCode += "D0";

            if (textToDisplayTB.Text.Length == 0)
            {
               
            }

            textCode += Convert.ToInt32(redSlider.Value).ToString("X2");
            textCode += Convert.ToInt32(blueSlider.Value).ToString("X2");
            textCode += Convert.ToInt32(greenSlider.Value).ToString("X2");
            textCode += "D1";

            if (textToDisplayTB.Text.Length == 0)
            {
                textCode += "40";
            }

            int interval = 0;
            switch (timeIntervalCB.SelectedIndex)
            {
                case 0:
                    {
                        interval = 1;
                        break;
                    }
                case 1:
                    {
                        interval = 2;
                        break;
                    }
                case 2:
                    {
                        interval = 4;
                        break;
                    }
                case 3:
                    {
                        interval = 8;
                        break;
                    }
            }

            foreach (char c in textToDisplayTB.Text)
            {
                for (int x = 0; x < interval; x++)
                {
                    if (c == ' ')
                    {
                        textCode += "40";
                    }
                    else
                    {
                    }
                        textCode += Convert.ToInt32(c).ToString("X2");
                }           
            }
            //space because of repeating
            //textCode += "40";
            
            textCode += "D2";
            textCode += "FF";
            comm.WriteData(textCode);
        }

        private void rbCheckedChanged(object sender, RoutedEventArgs e)
        {
           this.changeMode();
        }

        private void selectAnimation(object sender, RoutedEventArgs e)
        {
            string textCode = "C0";
            textCode += "E0";

            switch (Convert.ToInt16(((Button)sender).Tag))
            {
                case 0:
                    {
                        textCode += "07";
                        break;
                    }
                case 1:
                    {
                        textCode += "1B";
                        break;
                    }
                case 2:
                    {
                        textCode += "09";
                        break;
                    }
                case 3:
                    {
                        textCode += "03";
                        break;
                    }
                case 4:
                    {
                        textCode += "31";
                        break;
                    }
            }
           

            textCode += "E1";
            textCode += "FF";
            comm.WriteData(textCode);
        }
    }
}
