using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace Azyobuzi.Twirunrun
{
    public class ExceptionViewer : TextViewer
    {
        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.Button exitBtn;
        private System.Windows.Forms.Button closeBtn;
    
        private void InitializeComponent()
        {
            this.panel1 = new System.Windows.Forms.Panel();
            this.closeBtn = new System.Windows.Forms.Button();
            this.exitBtn = new System.Windows.Forms.Button();
            this.panel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // Txtbox
            // 
            this.Txtbox.Size = new System.Drawing.Size(284, 240);
            // 
            // ProgressBar
            // 
            this.ProgressBar.Visible = false;
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.exitBtn);
            this.panel1.Controls.Add(this.closeBtn);
            this.panel1.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.panel1.Location = new System.Drawing.Point(0, 240);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(284, 23);
            this.panel1.TabIndex = 4;
            // 
            // closeBtn
            // 
            this.closeBtn.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.closeBtn.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.closeBtn.Location = new System.Drawing.Point(134, 0);
            this.closeBtn.Margin = new System.Windows.Forms.Padding(0);
            this.closeBtn.Name = "closeBtn";
            this.closeBtn.Size = new System.Drawing.Size(75, 23);
            this.closeBtn.TabIndex = 0;
            this.closeBtn.Text = "閉じる";
            this.closeBtn.UseVisualStyleBackColor = true;
            this.closeBtn.Click += new System.EventHandler(this.closeBtn_Click);
            // 
            // exitBtn
            // 
            this.exitBtn.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.exitBtn.Location = new System.Drawing.Point(209, 0);
            this.exitBtn.Margin = new System.Windows.Forms.Padding(0);
            this.exitBtn.Name = "exitBtn";
            this.exitBtn.Size = new System.Drawing.Size(75, 23);
            this.exitBtn.TabIndex = 1;
            this.exitBtn.Text = "終了";
            this.exitBtn.UseVisualStyleBackColor = true;
            this.exitBtn.Click += new System.EventHandler(this.exitBtn_Click);
            // 
            // ExceptionViewer
            // 
            this.AcceptButton = this.closeBtn;
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.CancelButton = this.closeBtn;
            this.ClientSize = new System.Drawing.Size(284, 263);
            this.Controls.Add(this.panel1);
            this.Name = "ExceptionViewer";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "エラーが発生しました";
            this.TopMost = true;
            this.Controls.SetChildIndex(this.panel1, 0);
            this.Controls.SetChildIndex(this.Txtbox, 0);
            this.Controls.SetChildIndex(this.ProgressBar, 0);
            this.panel1.ResumeLayout(false);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        public ExceptionViewer(Exception ex)
        {
            InitializeComponent();

            var now = DateTime.Now;
            string outputTxt;
            if (ex != null)
            {
                outputTxt = string.Format(
                    @"エラーが発生しました{0}{1}{0}{0}{2}{0}{0}既に確認済みの問題の可能性がありますが@azyobuzinに連絡くれるとよろこびます。",
                    Environment.NewLine,
                    now,
                    ex.ToString()
                );
            }
            else
            {
                outputTxt = "エラーが発生しましたが、詳細な情報を取得できませんでした。\n\n" + now;
            }

            var filename = "TwirunrunError"
                + now.Year.ToString("0000")
                + now.Month.ToString("00")
                + now.Day.ToString("00")
                + now.DayOfWeek.ToString().Substring(0, 3)
                + now.Hour.ToString("00")
                + now.Minute.ToString("00")
                + now.Second.ToString("00")
                + ".txt";

            Directory.CreateDirectory(Application.StartupPath + "\\logs");
            File.WriteAllText(Application.StartupPath + "\\logs\\" + filename, outputTxt, Encoding.GetEncoding("shift_jis"));

            Txtbox.Text = outputTxt + string.Format("\n\n-----ログを{0}に保存しました。-----", filename);
        }

        private void closeBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void exitBtn_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
