using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PostCardHitsuji.Ui.Controls;
using PostCardHitsuji.Global;

namespace PostCardHitsuji
{
    public partial class Form1 : Form
    {
        private Graphics grap_;
        private Pen penOuter_;
        private Pen penInner_;
        private Brush brushOuter;
        private Brush brushInner;

        public Form1()
        {
            InitializeComponent();
            setWindowText("");

            buttonIllustLayout.BackColorType = Ui.New.DesignedLabel.BackColorTypes.YellowGreen;
            buttonReseiverLayout.BackColorType = Ui.New.DesignedLabel.BackColorTypes.YellowGreen;
            buttonMapping.BackColorType = Ui.New.DesignedLabel.BackColorTypes.YellowGreen;
            buttonBackup.BackColorType = Ui.New.DesignedLabel.BackColorTypes.Orange;
            buttonRefresh.BackColorType = Ui.New.DesignedLabel.BackColorTypes.Orange;

            grap_ = this.CreateGraphics();
            penOuter_ = new Pen(Brushes.Blue, 20);
            penInner_ = new Pen(Brushes.LightCoral, 20);
            brushOuter = Brushes.Blue;
            brushInner = Brushes.LightCoral;

            buttonPerple.Tag = new NewPersonUserControl();
            buttonAddress.Tag = new NewAddressUserControl();
            buttonGroup.Tag = new NewGroupUserControl();
            buttonIllustLayout.Tag = new NewIllustLayoutUserControl();
            buttonReseiverLayout.Tag = new NewReseiverLayoutUserControl();
            buttonMapping.Tag = new NewMappingLayoutUserControl();

            (buttonPerple.Tag as Control).Name = "＞人情報";
            (buttonAddress.Tag as Control).Name = "＞住所録";
            (buttonGroup.Tag as Control).Name = "＞グループ情報";
            (buttonIllustLayout.Tag as Control).Name = "＞イラストレイアウト設計";
            (buttonReseiverLayout.Tag as Control).Name = "＞宛名レイアウト設計";
            (buttonMapping.Tag as Control).Name = "＞宛名レイアウトマッピング";

            buttonPerple.OnClickedEvent += new EventHandler(buttonView_Clicked);
            buttonAddress.OnClickedEvent += new EventHandler(buttonView_Clicked);
            buttonFont.OnClickedEvent += new EventHandler(buttonView_Clicked);
            buttonGroup.OnClickedEvent += new EventHandler(buttonView_Clicked);
            buttonIllustLayout.OnClickedEvent += new EventHandler(buttonView_Clicked);
            buttonReseiverLayout.OnClickedEvent += new EventHandler(buttonView_Clicked);
            buttonMapping.OnClickedEvent += new EventHandler(buttonView_Clicked);

            buttonBackup.OnClickedEvent += new EventHandler(buttonDbBackup_Clicked);
            buttonRefresh.OnClickedEvent += new EventHandler(buttonDbRefresh_Clicked);
        }

        private void buttonView_Clicked(object sender, EventArgs ev)
        {
            this.buttonRefresh.Enabled = false;
            this.buttonRefresh.Visible = false;

            this.tableLayoutPanel4.SuspendLayout();

            if (tableLayoutPanel4.Controls.Count >= 1)
                tableLayoutPanel4.Controls.Clear();
            tableLayoutPanel4.Controls.Add((sender as Control).Tag as Control, 0, 0);
            ((sender as Control).Tag as Control).Dock = DockStyle.Fill;

            var title_ = new StringBuilder("年賀状ひつじ");
            this.Text = title_.Append(((sender as Control).Tag as Control).Name).ToString();
            setWindowText(((sender as Control).Tag as Control).Name);

            this.tableLayoutPanel4.ResumeLayout(false);
            this.tableLayoutPanel4.PerformLayout();
        }

        private void setWindowText(string suffix)
        {
            this.Text = new StringBuilder("年賀状ひつじ").Append(suffix).ToString();
        }

        private void buttonDbBackup_Clicked(object sender, EventArgs ev)
        {
            var dialog = PostCardHitsuji.Utils.WinControlAttacher.DialogAttacher.createFileSaveDialog("ファイル(*.DB)|*.DB");
            var result = dialog.ShowDialog(this);
            if (result == System.Windows.Forms.DialogResult.OK || 
                result == System.Windows.Forms.DialogResult.Yes)
            {
                if (dialog.FileName.Equals("")) return;

                if (System.IO.File.Exists(dialog.FileName))
                    System.IO.File.Delete(dialog.FileName);

                var current = System.IO.Directory.GetCurrentDirectory();
                System.IO.File.Copy(current + "\\DB\\POSTCARD_HITSUJI.DB", dialog.FileName);
            }
        }

        private void buttonDbRefresh_Clicked(object sender, EventArgs ev)
        {
            var msg = "バックアップは取りましたか？\r\nDB最適化よろしければ『はい』を選択してください。";
            if (MessageBox.Show(this, msg, "", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
                return;
            using (var daoFactory = new PostCardHitsuji.DataClient.DAO.DaoFactory())
            {
                var dbUtilDao = daoFactory.getDao<PostCardHitsuji.DataClient.DAO.DaoUtilContentModel>(GlobalSettings.dbUtilDao_);
                (dbUtilDao as PostCardHitsuji.DataClient.DAO.DbUtilDao).vacuum(null);
            }
        }


        private void button_InsertSampleResData_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(this, "宛名レイアウトのサンプルデータ登録", 
                                      "開発時のみ利用",
                                      MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.Cancel)
                return;

            using (var daoFactory = new PostCardHitsuji.DataClient.DAO.DaoFactory())
            {
                try
                {   //DAO生成
                    var dao = (PostCardHitsuji.DataClient.DAO.IllustLayoutDao)daoFactory.getDao<PostCardHitsuji.DataClient.Model.IllustLayoutContentModel>(PostCardHitsuji.Global.GlobalSettings.illLayoutDao_);
                    daoFactory.beginTransaction();
                    dao.Init_ReseiverLayoutSampleData(null);
                    daoFactory.commitTransaction();
                }
                catch (Exception ex)
                {
                    daoFactory.rollbackTransaction();
                    System.Windows.Forms.MessageBox.Show(ex.ToString());
                }
            }
        }

    }
}
