using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Price_Analytic_API_Demo
{
    public partial class FormMain : Form
    {
        public string _baseApiUrl = "https://app.price-analytic.com/WebApi/";
        //public string _baseApiUrl = "http://localhost:55966/WebApi/";
        
        WebClient _wc = new WebClient();

        string ApiKey
        {
            get
            {
                return textBoxApiKey.Text;
            }
        }

        public FormMain()
        {
            InitializeComponent();
        }

        private void buttonReportXmlLatestGet_Click(object sender, EventArgs e)
        {
            try
            {
                var u = UrlFormat("ReportXmlLatestGet");
                if (checkBoxXmlLatestWithTaskId.Checked)
                {
                    u += "?task_id=" + numericUpDownXmlTaskId.Value.ToString();
                }
                var data = _wc.DownloadData(u);
                var sfd = new System.Windows.Forms.SaveFileDialog();
                sfd.Filter = "*.xml|*.xml";
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    System.IO.File.WriteAllBytes(sfd.FileName, data);
                }
            }
            catch (Exception ex)
            {
                ExceptionHandle(ex);
            }
        }

        private void buttonReportXmlInfoLatestGet_Click(object sender, EventArgs e)
        {
            try
            {
                var u = UrlFormat("ReportXmlInfoLatestGet");
                if (checkBoxXmlInfoWithTaskId.Checked)
                {
                    u += "?task_id=" + numericUpDownXmlInfoTaskId.Value.ToString();
                }
                var r = _wc.DownloadString(u);
                MsgShow(r);
            }
            catch (Exception ex)
            {
                ExceptionHandle(ex);
            }
        }

        private void buttonProductsUpload_Click(object sender, EventArgs e)
        {
            try
            {
                var u = UrlFormat("ProductsUpload");

                var ofd = new System.Windows.Forms.OpenFileDialog();
                ofd.Filter = "*.yml|*.yml";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    var dataResp = _wc.UploadFile(u, ofd.FileName);
                    var r = System.Text.Encoding.UTF8.GetString(dataResp);
                    MsgShow(r);
                }
            }
            catch (Exception ex)
            {
                ExceptionHandle(ex);
            }
        }

        private void buttonReportCreate_Click(object sender, EventArgs e)
        {
            try
            {
                var u = UrlFormat("ReportCreate");
                var r = _wc.DownloadString(u);
                MsgShow(r);
            }
            catch (Exception ex)
            {
                ExceptionHandle(ex);
            }
        }

        string UrlFormat(string functionName)
        {
            return _baseApiUrl + functionName + "/" + ApiKey;
        }

        private void FormMain_Load(object sender, EventArgs e)
        {

        }

        private void FormMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            Properties.Settings.Default.Save();
        }

        void ExceptionHandle(Exception ex)
        {
            if (ex is WebException)
            {
                var wex = ex as WebException;
                var resp = new StreamReader(wex.Response.GetResponseStream()).ReadToEnd();
                MsgShow(ex.Message + "\r\n" + resp);
            }
            else
            {
                MsgShow(ex.Message);
            }
        }

        void MsgShow(string m)
        {
            var msg = new MsgForm();
            msg.TextMsg = m;
            msg.ShowDialog();
        }
    }
}
