using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using HtmlAgilityPack.Samples;
using kr.ac.kaist.swrc.jhannanum.comm;
using kr.ac.kaist.swrc.jhannanum.hannanum;

namespace DemoApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Workflow workflow = WorkflowFactory.getPredefinedWorkflow(WorkflowFactory.WORKFLOW_NOUN_EXTRACTOR);

            try
            {
                /* Activate the work flow in the thread mode */
                workflow.activateWorkflow(true);

                // string doc = GetWebPage("http://ko.wikipedia.org/wiki/%EC%9D%B8%ED%84%B0%EB%84%B7");

                //HtmlToText htt = new HtmlToText();

                //string document = htt.ConvertHtml(doc);

                string document = File.ReadAllText("sample.txt");
                StringBuilder sb = new StringBuilder();

                ///* Analysis using the work flow */
                //String document = "롯데마트가 판매하고 있는 흑마늘 양념 치킨이 논란이 되고 있다.\n";
                workflow.analyze(document);

                LinkedList<Sentence> resultList = workflow.getResultOfDocument(new Sentence(0, 0, false));
                foreach (Sentence s in resultList)
                {
                    Eojeol[] eojeolArray = s.Eojeols;
                    for (int i = 0; i < eojeolArray.Length; i++)
                    {
                        if (eojeolArray[i].length > 0)
                        {
                            String[] morphemes = eojeolArray[i].Morphemes;
                            for (int j = 0; j < morphemes.Length; j++)
                            {
                                sb.Append(morphemes[j]);
                                sb.Append(", ");
                            //    Trace.Write(morphemes[j]);
                            }
                            //Trace.Write(", ");
                        }
                    }
                }

                File.WriteAllText("sample.output.txt", sb.ToString());

                //Trace.WriteLine(Environment.NewLine);

                ///* Once a work flow is activated, it can be used repeatedly. */
                //document = "프로젝트 전체 회의.\n"
                //    + "회의 일정은 다음과 같습니다.\n"
                //    + "日時: 2010년 7월 30일 오후 1시\n"
                //    + "場所: Coex Conference Room\n";

                //workflow.analyze(document);

                //resultList = workflow.getResultOfDocument(new Sentence(0, 0, false));
                //foreach (Sentence s in resultList)
                //{
                //    Eojeol[] eojeolArray = s.Eojeols;
                //    for (int i = 0; i < eojeolArray.Length; i++)
                //    {
                //        if (eojeolArray[i].length > 0)
                //        {
                //            String[] morphemes = eojeolArray[i].Morphemes;
                //            for (int j = 0; j < morphemes.Length; j++)
                //            {
                //                Trace.Write(morphemes[j]);
                //            }
                //            Trace.Write(", ");
                //        }
                //    }
                //}
                //Trace.WriteLine(Environment.NewLine);

                workflow.close();

            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return;
            }

            /* Shutdown the work flow */
            workflow.close();
        }

        private static string GetWebPage(string url)
        {
            WebClient wc = new WebClient();

            byte[] docBytes = wc.DownloadData(url);
            string encodeType = wc.ResponseHeaders["Content-Type"];

            string charsetKey = "charset";
            int pos = encodeType.IndexOf(charsetKey);

            Encoding currentEncoding = Encoding.Default;
            if (pos != -1)
            {
                pos = encodeType.IndexOf("=", pos + charsetKey.Length);
                if (pos != -1)
                {
                    string charset = encodeType.Substring(pos + 1);
                    currentEncoding = Encoding.GetEncoding(charset);
                }
            }

            return currentEncoding.GetString(docBytes);
        }
    }
}
