using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Translator.TranslatorService;

namespace Translator
{
    public partial class MainPage : UserControl
    {
        TranslatorService.LanguageServiceClient client = new TranslatorService.LanguageServiceClient();// Creating Bing SOAP Ref object
        string AppId = "6CE9C85A41571C050C379F60DA173D286384E0F2";// This AppId is created for my name
        System.Collections.ObjectModel.ObservableCollection<string> results;
        System.Collections.ObjectModel.ObservableCollection<string> langcodes;

        public MainPage()
        {
            InitializeComponent();

            //Event Handlers
            client.TranslateCompleted += new EventHandler<TranslateCompletedEventArgs>(client_TranslateCompleted);
            client.GetLanguageNamesCompleted += new EventHandler<GetLanguageNamesCompletedEventArgs>(client_GetLanguageNamesCompleted);
            client.GetLanguagesCompleted += new EventHandler<GetLanguagesCompletedEventArgs>(client_GetLanguagesCompleted);
            //For filling the dropdown's and Language codes
            client.GetLanguageNamesAsync(AppId, this.Language.IetfLanguageTag);
            client.GetLanguagesAsync(AppId);
        }


        //Get the translated text
        protected void client_TranslateCompleted(Object sender, TranslateCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                destTxt.Text = e.Result;
            }
        }

        //Get Languages
        protected void client_GetLanguagesCompleted(object sender, GetLanguagesCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                langcodes = e.Result;
            }
        }

        //Fill the dropdown's
        protected void client_GetLanguageNamesCompleted(Object sender, GetLanguageNamesCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                results = e.Result;
                this.src.ItemsSource = results;
                this.dest.ItemsSource = results;
            }
        }

        private void PerformTranslation()
        {
            if (src.SelectedItem != null && dest.SelectedItem != null)
            {
                client.TranslateAsync(AppId, srcTxt.Text, (langcodes[src.SelectedIndex].ToString()), (langcodes[dest.SelectedIndex].ToString()));
            }
            else
                MessageBox.Show("Please select languages first !");
        }

        //Translate on button click
        private void Translate_Click(object sender, RoutedEventArgs e)
        {
            PerformTranslation();
        }

        private void Invert_Click(object sender, RoutedEventArgs e)
        {
            if (src.SelectedItem != null && dest.SelectedItem != null)
            {
                string tmp = src.SelectedItem.ToString();
                src.SelectedItem = dest.SelectedItem.ToString();
                dest.SelectedItem = tmp;
            }
        }

        private void CopyText_Click(object sender, RoutedEventArgs e)
        {
            Clipboard.SetText(destTxt.Text);
        }

        private void Install_Click(object sender, RoutedEventArgs e)
        {
            if (!Application.Current.IsRunningOutOfBrowser)
            { Application.Current.Install(); }
        }

    }
}