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

using nispi;

namespace nispi.OptionControl
{
    partial class OutWardTabData : CommonInEvent
    {
        private string tabFontName;
        private float tabFontSize;

        //タブの中身の文字色
        private byte tabForeColorRed;
        private byte tabForeColorGreen;
        private byte tabForeColorBlue;
        //タブの中身の背景色
        private byte tabBackColorRed;
        private byte tabBackColorGreen;
        private byte tabBackColorBlue;
        //タブのつまむ部分の色のグラデーション
        private byte tabSelectedColorRed;
        private byte tabSelectedColorGreen;
        private byte tabSelectedColorBlue;
        //タブのつまむ部分のグラデーションの終わりの色
        private byte tabHotColorRed;
        private byte tabHotColorGreen;
        private byte tabHotColorBlue;

        private Mutex mutex = new Mutex(false, "OutWardTabData");

        public OutWardTabData()
        {
            ConvertV1ToV2();
            GetXMLData();
        }
        
        public bool ConvertV1ToV2()
        {
            string temp = FileIO.ReadShiftJis("setting", "outward_tab2.xml");
            if ((temp == null) || (temp.Length == 0))
            {

                mutex.WaitOne();
                //バージョン１のデータから背景色と文字食だけを得る。
                string str = FileIO.ReadShiftJis("setting", "outward_tab.xml");

                if ((str == null) || (str.Length == 0))
                    str = ResetXML();

                //mutex.WaitOne();
                try
                {
                    XNamespace ns = "OutWard_Tab";
                    XDocument doc = XDocument.Parse(str);
                    XElement doc2 = doc.Element(ns + "Option");

                    TabFontName = doc2.Element(ns + "TabFontName").Value;
                    TabFontSize = float.Parse(doc2.Element(ns + "TabFontSize").Value);

                    TabForeColorRed = Byte.Parse(doc2.Element(ns + "TabForeColorRed").Value);
                    TabForeColorGreen = Byte.Parse(doc2.Element(ns + "TabForeColorGreen").Value);
                    TabForeColorBlue = Byte.Parse(doc2.Element(ns + "TabForeColorBlue").Value);

                    TabBackColorRed = Byte.Parse(doc2.Element(ns + "TabBackColorRed").Value);
                    TabBackColorGreen = Byte.Parse(doc2.Element(ns + "TabBackColorGreen").Value);
                    TabBackColorBlue = Byte.Parse(doc2.Element(ns + "TabBackColorBlue").Value);
                }
                catch { ;}

                this.TabSelectedColorRed = TabBackColorRed;
                this.TabSelectedColorGreen = TabBackColorGreen;
                this.TabSelectedColorBlue = TabBackColorBlue;

                this.TabHotColorRed = TabBackColorRed;
                this.TabHotColorGreen = TabBackColorGreen;
                this.TabHotColorBlue = TabBackColorBlue;

                XNamespace nsz = "OutWard_Tab";
                XElement docz = new XElement(nsz + "Option",
                    new XElement(nsz + "TabFontName", TabFontName),
                    new XElement(nsz + "TabFontSize", TabFontSize),

                    new XElement(nsz + "TabForeColorRed", TabForeColorRed),
                    new XElement(nsz + "TabForeColorGreen", TabForeColorGreen),
                    new XElement(nsz + "TabForeColorBlue", TabForeColorBlue),

                    new XElement(nsz + "TabBackColorRed", TabBackColorRed),
                    new XElement(nsz + "TabBackColorGreen", TabBackColorGreen),
                    new XElement(nsz + "TabBackColorBlue", TabBackColorBlue),

                    new XElement(nsz + "TabSelectedColorRed", TabSelectedColorRed),
                    new XElement(nsz + "TabSelectedColorGreen", TabSelectedColorGreen),
                    new XElement(nsz + "TabSelectedColorBlue", TabSelectedColorBlue),

                    new XElement(nsz + "TabHotColorRed", TabHotColorRed),
                    new XElement(nsz + "TabHotColorGreen", TabHotColorGreen),
                    new XElement(nsz + "TabHotColorBlue", TabHotColorBlue));

                FileIO.SaveShiftJis("setting", "outward_tab2.xml", docz.ToString());
                mutex.ReleaseMutex();

            }

            return true;
        }

        private string ResetXML()
        {
            XNamespace ns = "OutWard_Tab";
            XElement doc = new XElement(ns + "Option",
                new XElement(ns + "TabFontName", "ＭＳ Ｐゴシック"),
                new XElement(ns + "TabFontSize", "12.0"),

                new XElement(ns + "TabForeColorRed", SystemColors.WindowText.R.ToString()),
                new XElement(ns + "TabForeColorGreen", SystemColors.WindowText.G.ToString()),
                new XElement(ns + "TabForeColorBlue", SystemColors.WindowText.B.ToString()),

                new XElement(ns + "TabBackColorRed", SystemColors.Control.R.ToString()),
                new XElement(ns + "TabBackColorGreen", SystemColors.Control.G.ToString()),
                new XElement(ns + "TabBackColorBlue", SystemColors.Control.B.ToString()),

                new XElement(ns + "TabSelectedColorRed", SystemColors.Control.R.ToString()),
                new XElement(ns + "TabSelectedColorGreen", SystemColors.Control.G.ToString()),
                new XElement(ns + "TabSelectedColorBlue", SystemColors.Control.B.ToString()),

                new XElement(ns + "TabHotColorRed", SystemColors.Control.R.ToString()),
                new XElement(ns + "TabHotColorGreen", SystemColors.Control.G.ToString()),
                new XElement(ns + "TabHotColorBlue", SystemColors.Control.B.ToString()));

            FileIO.SaveShiftJis("setting", "outward_tab2.xml", doc.ToString());

            return doc.ToString();
        }

        private void GetXMLData()
        {
            mutex.WaitOne();
            string str = FileIO.ReadShiftJis("setting", "outward_tab2.xml");

            if ((str == null) || (str.Length == 0))
                str = ResetXML();

            //mutex.WaitOne();
            try
            {
                XNamespace ns = "OutWard_Tab";
                XDocument doc = XDocument.Parse(str);
                XElement doc2 = doc.Element(ns + "Option");

                TabFontName = doc2.Element(ns + "TabFontName").Value;
                TabFontSize = float.Parse(doc2.Element(ns + "TabFontSize").Value);

                TabForeColorRed = Byte.Parse(doc2.Element(ns + "TabForeColorRed").Value);
                TabForeColorGreen = Byte.Parse(doc2.Element(ns + "TabForeColorGreen").Value);
                TabForeColorBlue = Byte.Parse(doc2.Element(ns + "TabForeColorBlue").Value);

                TabBackColorRed = Byte.Parse(doc2.Element(ns + "TabBackColorRed").Value);
                TabBackColorGreen = Byte.Parse(doc2.Element(ns + "TabBackColorGreen").Value);
                TabBackColorBlue = Byte.Parse(doc2.Element(ns + "TabBackColorBlue").Value);

                TabSelectedColorRed = Byte.Parse(doc2.Element(ns + "TabSelectedColorRed").Value);
                TabSelectedColorGreen = Byte.Parse(doc2.Element(ns + "TabSelectedColorGreen").Value);
                TabSelectedColorBlue = Byte.Parse(doc2.Element(ns + "TabSelectedColorBlue").Value);

                TabHotColorRed = Byte.Parse(doc2.Element(ns + "TabHotColorRed").Value);
                TabHotColorGreen = Byte.Parse(doc2.Element(ns + "TabHotColorGreen").Value);
                TabHotColorBlue = Byte.Parse(doc2.Element(ns + "TabHotColorBlue").Value);
            }
            catch { ;}
            mutex.ReleaseMutex();
        }

        public void SaveTabToFile()
        {
            mutex.WaitOne();

            XNamespace ns = "OutWard_Tab";
            XElement doc = new XElement(ns + "Option",
                new XElement(ns + "TabFontName", TabFontName),
                new XElement(ns + "TabFontSize", TabFontSize),

                new XElement(ns + "TabForeColorRed", TabForeColorRed),
                new XElement(ns + "TabForeColorGreen", TabForeColorGreen),
                new XElement(ns + "TabForeColorBlue", TabForeColorBlue),

                new XElement(ns + "TabBackColorRed", TabBackColorRed),
                new XElement(ns + "TabBackColorGreen", TabBackColorGreen),
                new XElement(ns + "TabBackColorBlue", TabBackColorBlue),

                new XElement(ns + "TabSelectedColorRed", TabSelectedColorRed),
                new XElement(ns + "TabSelectedColorGreen", TabSelectedColorGreen),
                new XElement(ns + "TabSelectedColorBlue", TabSelectedColorBlue),

                new XElement(ns + "TabHotColorRed", TabHotColorRed),
                new XElement(ns + "TabHotColorGreen", TabHotColorGreen),
                new XElement(ns + "TabHotColorBlue", TabHotColorBlue));

            FileIO.SaveShiftJis("setting", "outward_tab2.xml", doc.ToString());

            mutex.ReleaseMutex();
        }

        public String TabFontName
        {
            set
            {
                mutex.WaitOne();
                this.tabFontName = value;
                mutex.ReleaseMutex();
            }
            get
            {
                mutex.WaitOne();
                string data = StrUtil.GetStrCopy(this.tabFontName);
                mutex.ReleaseMutex();
                return data;
            }
        }

        public float TabFontSize
        {
            set
            {
                mutex.WaitOne();
                this.tabFontSize = value;
                mutex.ReleaseMutex();
            }
            get
            {
                mutex.WaitOne();
                float data = this.tabFontSize;
                mutex.ReleaseMutex();
                return data;
            }
        }

        public byte TabForeColorRed
        {
            set
            {
                //if (CheckBool(value) == false) return;
                mutex.WaitOne();
                this.tabForeColorRed = value;
                mutex.ReleaseMutex();
            }
            get
            {
                mutex.WaitOne();
                byte data = this.tabForeColorRed;
                mutex.ReleaseMutex();
                return data;
            }
        }
        public byte TabForeColorGreen
        {
            set
            {
                //if (CheckBool(value) == false) return;
                mutex.WaitOne();
                this.tabForeColorGreen = value;
                mutex.ReleaseMutex();
            }
            get
            {
                mutex.WaitOne();
                byte data = this.tabForeColorGreen;
                mutex.ReleaseMutex();
                return data;
            }
        }
        public byte TabForeColorBlue
        {
            set
            {
                //if (CheckBool(value) == false) return;
                mutex.WaitOne();
                this.tabForeColorBlue = value;
                mutex.ReleaseMutex();
            }
            get
            {
                mutex.WaitOne();
                byte data = this.tabForeColorBlue;
                mutex.ReleaseMutex();
                return data;
            }
        }
        public byte TabBackColorRed
        {
            set
            {
                //if (CheckBool(value) == false) return;
                mutex.WaitOne();
                this.tabBackColorRed = value;
                mutex.ReleaseMutex();
            }
            get
            {
                mutex.WaitOne();
                byte data = this.tabBackColorRed;
                mutex.ReleaseMutex();
                return data;
            }
        }
        public byte TabBackColorGreen
        {
            set
            {
                //if (CheckBool(value) == false) return;
                mutex.WaitOne();
                this.tabBackColorGreen = value;
                mutex.ReleaseMutex();
            }
            get
            {
                mutex.WaitOne();
                byte data = this.tabBackColorGreen;
                mutex.ReleaseMutex();
                return data;
            }
        }
        public byte TabBackColorBlue
        {
            set
            {
                //if (CheckBool(value) == false) return;
                mutex.WaitOne();
                this.tabBackColorBlue = value;
                mutex.ReleaseMutex();
            }
            get
            {
                mutex.WaitOne();
                byte data = this.tabBackColorBlue;
                mutex.ReleaseMutex();
                return data;
            }
        }

    }
}
