//ライセンス情報はAgeOnフォルダのLISENCEフォルダを参照してください
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Drawing;
using System.Diagnostics;
using System.IO;

using MecInfoDll;

namespace nispi.Analysis
{
    /*class AnalysisImage0
    {
        public AnalysisImageControls analysisImageControls;

        public static string VIEWTYPE;
        public List<AnalysisImageList> analysisImageList;

        public int pageNum = 0;
        DatInfo dat;

        public static bool Visible = false;

        public AnalysisImage0()
        {
            //Const.form.mainForm.Name = "isPopUp";
            analysisImageControls = new AnalysisImageControls(this);
            VIEWTYPE = "Thumbnail";

            Visible = true;
        }

        public void Open(DatInfo dat)
        {
            try
            {
                this.dat = dat;
                this.pageNum = 1;
                analysisImageList = new List<AnalysisImageList>();

                GetImageHtmlListFromDat(dat);

                if ((VIEWTYPE.CompareTo("Link") == 0) || (VIEWTYPE.CompareTo("Thumbnail") == 0))
                    WriteImageListFromDat(1);
                else
                {
                    string dir = Util.DatUrlToDatDirPath(dat.dat_url);
                    string folder = Util.DatUrlToThreadID(dat.dat_url);

                    WriteImageListToFileFromDat();
                    Process.Start(dir + Path.DirectorySeparatorChar + folder + Path.DirectorySeparatorChar + VIEWTYPE + "1" +
                        ".html");
                }
            }
            catch (System.Exception ex)
            {
                Const.debugForm.OutPutDebug("Analysis", ex);
            }
        }
        //analysisImageListの初期化をせず特定のページを開くメソッド
        public void ChangePage(int pageNum)
        {
            try
            {
                this.pageNum = pageNum;
                if ((VIEWTYPE.CompareTo("Link") == 0) || (VIEWTYPE.CompareTo("Thumbnail") == 0))
                    WriteImageListFromDat(pageNum);
            }
            catch (System.Exception ex)
            {
                Const.debugForm.OutPutDebug("Analysis", ex);
            }
        }
        //ページを指定しないメソッド
        public void OpenByBrowser()
        {
            try
            {
                string dir = Util.DatUrlToDatDirPath(dat.dat_url);
                string folder = Util.DatUrlToThreadID(dat.dat_url);

                WriteImageListToFileFromDat();
                Process.Start(dir + Path.DirectorySeparatorChar + folder + Path.DirectorySeparatorChar + VIEWTYPE + "1" +
                    ".html");
            }
            catch (System.Exception ex)
            {
                Const.debugForm.OutPutDebug("Analysis", ex);
            }
        }

        public delegate void OpenByBrowser_Delegate();
        public delegate void ChangePage_Delegate(int pageNum);
        public delegate void Worker_Delegate(DatInfo dat);

        public void WriteImageListFromDat(int pageNum)
        {
            AnalysisImageHtmlWriter htmlWriter = new AnalysisImageHtmlWriter(analysisImageList, dat, VIEWTYPE);
            string htmlData = htmlWriter.WriteImageListFromDat(pageNum);

            //analysisImageControls.browserParts.Size =
            //    new Size(analysisImageControls.aiFormParts.Size.Width - 4, analysisImageControls.aiFormParts.Size.Height - 29);

            //analysisImageControls.browserParts.Write(""); 
            //analysisImageControls.browserParts.AllowNavigation = false;
            //analysisImageControls.browserParts.Stop();
            //analysisImageControls.browserParts.Stop();
            analysisImageControls.browserParts.BrowserClear();
            //analysisImageControls.browserParts.AllowNavigation = true;
            analysisImageControls.browserParts.Write(htmlData);

            
            //analysisImageControls.browserParts.control.Stop();
        }

        public void WriteImageListToFileFromDat()
        {
            AnalysisImageHtmlWriter htmlWriter = new AnalysisImageHtmlWriter(analysisImageList, dat, VIEWTYPE);
            htmlWriter.WriteImageListToFileFromDat();
        }

        public void GetImageHtmlListFromDat(DatInfo dat)
        {

            Regex jpgRegex = new Regex("t?tp://([-_.!~*\'()a-zA-Z0-9;/?:@&=+$$,%#]+jpg)");
            Regex jpegRegex = new Regex("t?tp://([-_.!~*\'()a-zA-Z0-9;/?:@&=+$$,%#]+jpeg)");
            Regex pngRegex = new Regex("t?tp://([-_.!~*\'()a-zA-Z0-9;/?:@&=+$$,%#]+png)");
            Regex gifRegex = new Regex("t?tp://([-_.!~*\'()a-zA-Z0-9;/?:@&=+$$,%#]+gif)");
            Regex bmpRegex = new Regex("t?tp://([-_.!~*\'()a-zA-Z0-9;/?:@&=+$$,%#]+bmp)");
            foreach (Comment comment in dat.comments)
            {

                MatchCollection jpgMC = jpgRegex.Matches(comment.body);
                MatchCollection jpegMC = jpegRegex.Matches(comment.body);
                MatchCollection pngMC = pngRegex.Matches(comment.body);
                MatchCollection gifMC = gifRegex.Matches(comment.body);
                MatchCollection bmpMC = bmpRegex.Matches(comment.body);

                if (jpgMC.Count > 0)
                {
                    for (int num = 0; jpgMC.Count > num; num++)
                    {
                        if (jpgMC[num].Success == true)
                            analysisImageList.Add(new AnalysisImageList(jpgMC[num].Groups[0].Value, comment.num));
                    }
                }
                else if (jpegMC.Count > 0)
                {
                    for (int num = 0; jpegMC.Count > num; num++)
                    {
                        if (jpegMC[num].Success == true)
                            analysisImageList.Add(new AnalysisImageList(jpegMC[num].Groups[1].Value, comment.num));
                    }
                }
                else if (pngMC.Count > 0)
                {
                    for (int num = 0; pngMC.Count > num; num++)
                    {
                        if (pngMC[num].Success == true)
                            analysisImageList.Add(new AnalysisImageList(pngMC[num].Groups[1].Value, comment.num));
                    }
                }
                else if (gifMC.Count > 0)
                {
                    for (int num = 0; gifMC.Count > num; num++)
                    {
                        if (gifMC[num].Success == true)
                            analysisImageList.Add(new AnalysisImageList(gifMC[num].Groups[1].Value, comment.num));
                    }
                }
                else if (bmpMC.Count > 0)
                {
                    for (int num = 0; bmpMC.Count > num; num++)
                    {
                        if (bmpMC[num].Success == true)
                            analysisImageList.Add(new AnalysisImageList(bmpMC[num].Groups[1].Value, comment.num));
                    }
                }
            }
        }
        public string CapText(Match m)
        {
            return m.Value.ToString();
        }

        public void Cloose()
        {
            try
            {
                dat.comments.Clear();
                dat = null;
            }
            catch (System.Exception ex)
            {
                Const.debugForm.OutPutDebug("Analysis", ex);
            }

            try
            {
                this.analysisImageList.Clear();
            }
            catch (System.Exception ex)
            {
                Const.debugForm.OutPutDebug("Analysis", ex);
            }

            try
            {
                this.analysisImageList = null;

                this.analysisImageControls.RemoveControls();

                this.analysisImageControls.aiFormParts.Owner = null;
            }
            catch (System.Exception ex)
            {
                Const.debugForm.OutPutDebug("Analysis", ex);
            }
            //Const.form.mainForm.Name = "mainForm";
            Visible = false;
        }
    }*/

    class AnalysisImageList
    {
        public string imageAddress;
        public string num;
        public AnalysisImageList(string text)
        {
            imageAddress = text;
        }
        public AnalysisImageList(string text, string num)
        {
            imageAddress = text;
            this.num = num;
        }
    }
}