using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace Azyobuzi.Twirunrun
{
    public partial class TextViewer : Form
    {
        /*メモ
         * LoadProcessは非同期で実行される
         */

        public TextViewer()
        {
            InitializeComponent();
        }

        public Func<BackgroundWorker, string> LoadProcess { set; get; }

        private void TextViewer_Load(object sender, System.EventArgs e)
        {
            Settings.Instance.ViewerForm.Restore(this);
        }

        private void TextViewer_BoundsChanged(object sender, System.EventArgs e)
        {
            if (WindowState == FormWindowState.Normal)
                Settings.Instance.ViewerForm = FormSettings.FromForm(this);
            Settings.Instance.ViewerForm.WindowState = this.WindowState;
        }

        private void TextViewer_Shown(object sender, EventArgs e)
        {
            if (LoadProcess == null)
            {
                ProgressBar.Visible = false;
            }
            else
            {
                var bw = new BackgroundWorker();
                bw.WorkerReportsProgress = true;

                bw.DoWork += (_sender, _e) =>
                {
                    _e.Result = LoadProcess(bw);
                };

                bw.ProgressChanged += (_sender, _e) =>
                {
                    ProgressBar.Value = _e.ProgressPercentage;
                };

                bw.RunWorkerCompleted += (_sender, _e) =>
                {
                    Txtbox.Text = (string)_e.Result;
                    ProgressBar.Visible = false;
                };

                bw.RunWorkerAsync();
            }
        }
    }
}
