using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
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;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ThesisApp
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            var key = textBox1.Text;
            var raw = richTextBox1.Text;
            var server_url = textBox2.Text;


            Label process = new Label();
            create_label(30, 300, "Processing...!", process);

            //Prompt.ShowDialog("Test", "123");
            MessageBox.Show("Processing! Extracting Positive Phrases and TFDIF Scores!");
            //get_initial_data(key, raw, server_url);

            string url = String.Format("http://{0}:8080/get_initial_data?title={1}&raw={2}", server_url, key, raw);
            WebRequest myReq = WebRequest.Create(url);
            myReq.Timeout = 900000;
            WebResponse wr = myReq.GetResponse();
            Stream receiveStream = wr.GetResponseStream();
            StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);

            string content = reader.ReadToEnd();

            var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(content);

            var _summary = "";

            foreach (var x in dict)
            {
                //var k = x.Key;
                //var v = x.Value;
                if (x.Key.Equals("scores"))
                {
                    //string all_data = "Score Details: \n\n";
                    //var list_score = x.Value.Split('£');
                    //foreach(var score in list_score)
                    //{
                    //    var values = score.Split('¬');
                    //    all_data += "TFIDF Score: " + values[0] + "\n";
                    //    all_data += "Noun_Verb Score: " + values[1] + "\n";
                    //    all_data += "Jaccard Simlaroty Score: " + values[2] + "\n";
                    //    all_data += "Stopwords Score: " + values[3] + "\n";
                    //    all_data += "Phrase Score: " + values[4] + "\n";
                    //    all_data += "Total Score: " + values[5] + "\n";
                    //    all_data += "Sentence: " + values[6] + "\n\n";
                    //}

                }
                if (x.Key.Equals("summary"))
                {
                    _summary = x.Value;
                }

                if (x.Key.Equals("positive_phrases"))
                {
                    //var positive_phrase = x.Value.Split('¬');
                    //Label positive_label = new Label();
                    //create_label(30, 330,  "Positive Phrases: \n" + string.Join(", ", positive_phrase), positive_label);
                    MessageBox.Show("Positive Phrases and TFDIF Scores Extracted! Extracting MCQ' !");
                }
            }

            url = String.Format("http://{0}:8080/get_keys_data", server_url);
            myReq = WebRequest.Create(url);
            myReq.Timeout = 900000;
            wr = myReq.GetResponse();
            receiveStream = wr.GetResponseStream();
            reader = new StreamReader(receiveStream, Encoding.UTF8);
            content = reader.ReadToEnd();
            dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(content);

            var keys = "";
            foreach (var x in dict)
            {
                if (x.Key.Equals("keys"))
                {
                    keys = x.Value;
                }
            }

            var filename_ = key + "_" + RandomString(5) + ".txt";
            var tw = new StreamWriter(filename_, true);
            tw.Close();

            int x_panel = 0;
            var list_keys = keys.Split('¬');
            for (int i = 0; i < list_keys.Length; i++)
            {
                url = String.Format("http://{0}:8080/get_mcq?key={1}&summary={2}", server_url, list_keys[i], _summary);
                myReq = WebRequest.Create(url);
                myReq.Timeout = 900000;
                wr = myReq.GetResponse();
                receiveStream = wr.GetResponseStream();
                reader = new StreamReader(receiveStream, Encoding.UTF8);
                content = reader.ReadToEnd();
                dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(content);

                var question = "";
                var answer = "";
                var options = "";
                foreach (var x in dict)
                {
                    if (x.Key.Equals("Question"))
                    {
                        question = x.Value;
                    }
                    if (x.Key.Equals("Answer"))
                    {
                        answer = x.Value;
                    }
                    if (x.Key.Equals("Distractors"))
                    {
                        options = x.Value;
                    }
                }

                string[] options_label = { "a", "b", "c", "d", "e", "f", "g" };

                if (!question.Equals(""))
                {
                    var output = "";
                    output += "Question: " + question + "\n";
                    output += "Options: ";
                    var _options = options.Split('¬');

                    var all_options = "";
                    for(int k = 0; k < _options.Length; k++)
                    {
                        all_options += options_label[k] + ") " + _options[k] + "  ";
                    }
                    output += all_options + "\n";
                    output += "Answer: " + answer + "\n\n";

                    using (StreamWriter w = File.AppendText(filename_))
                    {
                        w.WriteLine(output);
                    }


                    Label mylab = new Label();
                    mylab.Text = output;
                    mylab.Location = new Point(0, x_panel);
                    mylab.AutoSize = true;
                    //mylab.Width = 300;
                    mylab.Font = new Font("Calibri", 10);
                    mylab.BorderStyle = BorderStyle.Fixed3D;
                    mylab.ForeColor = Color.Black;
                    mylab.Padding = new Padding(6);

                    // Adding this control to the form 
                    this.panel2.Controls.Add(mylab);
                    x_panel += 100;
                    MessageBox.Show("MCQ Loaded!");
                }
            }
        }

        private static Random random = new Random();
        public static string RandomString(int length)
        {
            const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            return new string(Enumerable.Repeat(chars, length)
              .Select(s => s[random.Next(s.Length)]).ToArray());
        }

        void get_initial_data(string key, string raw, string server_url)
        {
            string url = String.Format("http://{0}:8080/get_initial_data?title={1}&raw={2}", server_url, key, raw);
             WebRequest myReq = WebRequest.Create(url);

            WebResponse wr = myReq.GetResponse();
            Stream receiveStream = wr.GetResponseStream();
            StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);


            string content = reader.ReadToEnd();

            var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(content);

            foreach(var x in dict)
            {
                //var k = x.Key;
                //var v = x.Value;
                if (key.Equals("scores"))
                {
                    //scores = x.Value;
                }
                if (key.Equals("summary"))
                {
                    //summary = x.Value;
                }

                if (key.Equals("positive_phrases"))
                {
                    var positive_phrase = x.Value.Split('¬');
                    Label positive_label = new Label();
                    create_label(30, 330, string.Join(", ", positive_phrase), positive_label);
                }
            }

            MessageBox.Show("Processing Completed! Show Processed Data!");


            //var postive_phrases_list = positive_phrase.Split('¬');
            //this.label4.Text = string.Join(", ", postive_phrases_list);

            //this.label7.Text = summary;
        }

        public void create_label(int x, int y, string Text, Label mylab)
        {
            // Creating and setting the label 
            //Label mylab = new Label();
            mylab.Text = Text;
            mylab.Location = new Point(x,y);
            mylab.AutoSize = true;
            //mylab.Width = 300;
            mylab.Font = new Font("Calibri", 10);
            mylab.BorderStyle = BorderStyle.Fixed3D;
            mylab.ForeColor = Color.Black;
            mylab.Padding = new Padding(6);

            // Adding this control to the form 
            this.Controls.Add(mylab);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        //public static class Prompt
        //{
        //    public static int ShowDialog(string text, string caption)
        //    {
        //        Form prompt = new Form();
        //        prompt.Width = 500;
        //        prompt.Height = 100;
        //        prompt.Text = caption;
        //        Label textLabel = new Label() { Left = 50, Top = 20, Text = text };
        //       // NumericUpDown inputBox = new NumericUpDown() { Left = 50, Top = 50, Width = 400 };
        //        Button confirmation = new Button() { Text = "Continue,,,", Left = 350, Width = 100, Top = 70 };
        //        confirmation.Click += (sender, e) => { prompt.Close(); };
        //        prompt.Controls.Add(confirmation);
        //        prompt.Controls.Add(textLabel);
        //        //prompt.Controls.Add(inputBox);
        //        prompt.ShowDialog();
        //        return (int)0;
        //    }
        //}
    }
}
