// Copyright (C) 2010 panacoran <panacoran@users.sourceforge.jp>
// 
// This program is part of Protra.
//
// Protra is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, see <http://www.gnu.org/licenses/>.
// 
// $Id: ConfigBase.cs 426 2011-03-26 06:43:21Z panacoran $

using System;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Xml.Serialization;
using Protra.Lib.Dialogs;

namespace Protra.Lib.Config
{
    /// <summary>
    /// 設定ファイルを読み書きする抽象クラス。
    /// </summary>
    abstract public class ConfigBase
    {
        /// <summary>
        /// 古い設定ファイルを読み込む。
        /// </summary>
        abstract protected void ReadOldConfig();

        /// <summary>
        /// 設定ファイルの名前を取得する。
        /// </summary>
        abstract protected string ConfigName
        {
            get;
        }

        private string filename;
        private XmlSerializer serializer;
        FileSystemWatcher watcher;

        /// <summary>
        /// コンストラクタ。
        /// </summary>
        /// <param name="watch">ファイルの変更を監視して再読み込みするかを指定。</param>
        public ConfigBase(bool watch)
        {
            filename = Path.Combine(Global.DirConf, ConfigName + ".xml");
            serializer = new XmlSerializer(GetType());
            if (!Directory.Exists(Global.DirConf))
                Directory.CreateDirectory(Global.DirConf);
            if (watch)
            {
                watcher = new FileSystemWatcher(Global.DirConf);
                watcher.Changed += new FileSystemEventHandler(watcher_Changed);
                watcher.Filter = ConfigName + ".xml";
                watcher.EnableRaisingEvents = true;
            }
        }

        void watcher_Changed(object sender, FileSystemEventArgs e)
        {
            Load();
        }

        /// <summary>
        /// 設定ファイルを読み込む。
        /// </summary>
        public virtual void Load()
        {
            IOException ioex = null;
            for (var i = 0; i < 10; i++)
            {
                try
                {
                    using (var reader = new StreamReader(filename))
                    {
                        Object tmp = serializer.Deserialize(reader);
                        foreach (PropertyInfo property in this.GetType().GetProperties())
                            property.SetValue(this, property.GetValue(tmp, null), null);
                    }
                    return;
                }
                catch (FileNotFoundException)
                {
                    ReadOldConfig();
                    Save();
                    return;
                }
                catch (IOException e)
                {
                    ioex = e;
                    Thread.Sleep(200);
                }
                catch (InvalidOperationException e)
                {
                    using (var dialog = new ApplicationError())
                    {
                        dialog.ErrorMessage = "設定ファイルが壊れています。\n" + filename + "\n" + e.Message;
                        dialog.ShowDialog();
                    }
                    return;
                }
            }
            using (var dialog = new ApplicationError())
            {
                dialog.ErrorMessage = "設定ファイルの読み込みに失敗しました。\n" +
                    (ioex != null ? ioex.Message : filename);
                dialog.ShowDialog();
            }
        }

        /// <summary>
        /// 設定ファイルを書き込む。
        /// </summary>
        public virtual void Save()
        {
            try
            {
                if (watcher != null)
                    watcher.EnableRaisingEvents = false;
                using (StreamWriter writer = new StreamWriter(filename))
                    serializer.Serialize(writer, this);
            }
            catch
            {
                using (ApplicationError dialog = new ApplicationError())
                {
                    dialog.ErrorMessage = "設定ファイルの書き込みに失敗しました。\n" + filename;
                    dialog.Mode = ApplicationError.ErrorType.Recoverable;
                    dialog.ShowDialog();
                }
            }
            finally
            {
                if (watcher != null)
                    watcher.EnableRaisingEvents = true;
            }
        }
    }
}
