using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows;
using TencentCloud.Common;
using TencentCloud.Common.Profile;
using TencentCloud.Tmt.V20180321;
using TencentCloud.Tmt.V20180321.Models;

namespace QcloudTranslate
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            id.Text = Read().secret_id;
            key.Text = Read().secret_key;
        }

        [Serializable]
        private class Info
        {
            public string secret_id="Secret ID";
            public string secret_key= "Secret Key";

        }

        private void Write()
        {
            string filePath = "config.bin";
            Info info = new Info { secret_id = id.Text, secret_key = key.Text };
            using (FileStream fs = new FileStream(filePath, FileMode.Create))
            {
                BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(fs, info);
            }
        }
        Info Read()
        {
            string filePath = "config.bin";
            Info info;
            try
            {
                using (FileStream fs = new FileStream(filePath, FileMode.Open))
                {
                    BinaryFormatter bf = new BinaryFormatter();
                    info = bf.Deserialize(fs) as Info;
                    return info;
                }
            }catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return new Info();
            }
            
            
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string result = Tranlate(src.Text);
            tgt.Text = result;
            
        }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            Write();
        }

        string Tranlate(string source)
        {
            try
            {
                Credential cred = new Credential
                {
                    SecretId = id.Text,
                    SecretKey = key.Text
                };

                ClientProfile clientProfile = new ClientProfile();
                HttpProfile httpProfile = new HttpProfile();
                httpProfile.Endpoint = ("tmt.tencentcloudapi.com");
                clientProfile.HttpProfile = httpProfile;

                TmtClient client = new TmtClient(cred, "ap-shanghai", clientProfile);
                TextTranslateRequest req = new TextTranslateRequest();
                string strParams = "{\"SourceText\":\"" + source + "\",\"Source\":\"auto\",\"Target\":\"zh\",\"ProjectId\":0}";
                req = TextTranslateRequest.FromJsonString<TextTranslateRequest>(strParams);
                TextTranslateResponse resp = client.TextTranslateSync(req);
                Console.WriteLine(AbstractModel.ToJsonString(resp));
                //return AbstractModel.ToJsonString(resp);
                return resp.TargetText;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return e.ToString();
            }
        }
    }

}
