using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 System.IO;
using NetClient;
using System.Diagnostics;

namespace TalkDemo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private TalkClient m_Client = null;
        public MainWindow()
        {
            LoadLanguage();
            InitializeComponent();
            try
            {
                m_Client = new TalkClient();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                Process.GetCurrentProcess().Kill();
            }
            this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            login_btn.IsEnabled = true;
            logout_btn.IsEnabled = false;
            type_combox.IsEnabled = false;
            channel_combox.IsEnabled = false;
            start_btn.IsEnabled = false;
            stop_btn.IsEnabled = false;
        }

        private void LoadLanguage()
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + "Language";
            string fileNamePath = string.Empty;
            ResourceDictionary resourceDictionary = new ResourceDictionary();
            switch (System.Globalization.CultureInfo.CurrentCulture.LCID)
            {
                case 0x00804:
                    fileNamePath = path + "\\zh-cn.xaml";
                    if (!File.Exists(fileNamePath))
                    {
                        resourceDictionary.Source = new Uri("pack://application:,,,/language/zh-cn.xaml");
                    }
                    else
                    {
                        resourceDictionary.Source = new Uri(fileNamePath, UriKind.RelativeOrAbsolute);
                    }
                    break;
                default:
                    fileNamePath = path + "\\en-us.xaml";
                    if (!File.Exists(fileNamePath))
                    {
                        resourceDictionary.Source = new Uri("pack://application:,,,/language/en-us.xaml");
                    }
                    else
                    {
                        resourceDictionary.Source = new Uri(fileNamePath, UriKind.RelativeOrAbsolute);
                    }
                    break;
            }
            App.Current.Resources.MergedDictionaries.Add(resourceDictionary);
        }

        private void Port_Input(object sender, TextCompositionEventArgs e)
        {
            try
            {
                Convert.ToUInt32(e.Text);
                e.Handled = false;
            }
            catch
            {
                e.Handled = true;
            }
        }

        private void Login_Clicked(object sender, RoutedEventArgs e)
        {
            string ip = ip_textbox.Text.Trim();
            if (ip == "" || ip == null)
            {
                MessageBox.Show((string)App.Current.Resources["ID_IP_ERROR"]);
                return;
            }
            ushort port = 0;
            string portString = port_textbox.Text.Trim();
            if (portString == null || portString == "")
            {
                MessageBox.Show((string)App.Current.Resources["ID_PORT_ERROR1"]);
                return;
            }
            try
            {
                port = Convert.ToUInt16(port_textbox.Text.Trim());
            }
            catch
            {
                MessageBox.Show((string)App.Current.Resources["ID_PORT_ERROR2"]);
                return;
            }
            string username = username_textbox.Text.Trim();
            if (username == null || username == "")
            {
                MessageBox.Show((string)App.Current.Resources["ID_USERNAME_ERROR"]);
                return;
            }
            string password = password_textbox.Password.Trim();
            try
            {
                m_Client.Login(ip, port, username, password);
                type_combox.Items.Add((string)App.Current.Resources["ID_MODE1"]);
                type_combox.Items.Add((string)App.Current.Resources["ID_MODE2"]);
                type_combox.SelectedIndex = 0;
                login_btn.IsEnabled = false;
                logout_btn.IsEnabled = true;
                type_combox.IsEnabled = true;
                start_btn.IsEnabled = true;
                
            }
            catch (NETClientExcetion ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }

        }

        private void Logout_Clicked(object sender, RoutedEventArgs e)
        {
            m_Client.Logout();
            type_combox.Items.Clear();
            channel_combox.Items.Clear();
            login_btn.IsEnabled = true;
            logout_btn.IsEnabled = false;
            type_combox.IsEnabled = false;
            channel_combox.IsEnabled = false;
            start_btn.IsEnabled = false;
            stop_btn.IsEnabled = false;
        }

        private void Start_Clicked(object sender, RoutedEventArgs e)
        {
            try
            {
                int channel = 0;
                if (channel_combox.SelectedItem != null)
                {
                    channel = Convert.ToInt32(channel_combox.SelectedItem);
                }
                m_Client.Start((EM_TALK_TRANSFER_MODE)type_combox.SelectedIndex, channel);
                start_btn.IsEnabled = false;
                stop_btn.IsEnabled = true;
            }
            catch (NETClientExcetion ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Stop_Clicked(object sender, RoutedEventArgs e)
        {
            m_Client.Stop();
            start_btn.IsEnabled = true;
            stop_btn.IsEnabled = false;
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            base.OnClosing(e);
            m_Client = null;
            GC.Collect();
        }

        private void SelectedType_Changed(object sender, SelectionChangedEventArgs e)
        {
            if (type_combox.SelectedIndex == 1)
            {
                channel_combox.IsEnabled = true;
                for (int i = 0; i < m_Client.DeviceInfo.nChanNum; i++)
                {
                    channel_combox.Items.Add(i);
                }
                channel_combox.SelectedIndex = 0;
            }
            else
            {
                channel_combox.IsEnabled = false;
                channel_combox.Items.Clear();
            }
        }
    }
}
