using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace shuchancontrol
{
    public partial class Form1 : Form
    {
        ChessMain chessMain;
        public StringBuilder ChessRecord { get; set; }

        public Form1()
        {
            InitializeComponent();
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            chessMain = new ChessMain(Properties.Settings.Default.ComPort,
                Properties.Settings.Default.MovePositionPythonScriptPath,
                Properties.Settings.Default.ChessEnginePath,
                Properties.Settings.Default.ChessEngineArguments,
                Properties.Settings.Default.UserPlayGetterPath);
            chessMain.MessageToGui = new Queue<string>();
            chessMain.OccuredMoveToGui = new Queue<PieceMoveToGui>();
            chessMain.Board.MoveComputingHelperExe = Properties.Settings.Default.MoveComputingHelperExe;
            chessMain.Board.MoveComputingHelperArgument = Properties.Settings.Default.MoveComputingHelperArgument;
            timerGetLog.Start();
            chessMain.Run();
        }

        private void buttonStartConfiguration_Click(object sender, EventArgs e)
        {
            FormStartConfiguration fsc = new FormStartConfiguration();
            fsc.textBoxChessEngineArguments.Text = Properties.Settings.Default.ChessEngineArguments;
            fsc.textBoxChessEnginePath.Text = Properties.Settings.Default.ChessEnginePath;
            fsc.textBoxComPort.Text = Properties.Settings.Default.ComPort;
            fsc.textBoxMoveComputingHelperArgument.Text = Properties.Settings.Default.MoveComputingHelperArgument;
            fsc.textBoxMoveComputingHelperExe.Text = Properties.Settings.Default.MoveComputingHelperExe;
            fsc.textBoxMovePositionPythonScriptPath.Text = Properties.Settings.Default.MovePositionPythonScriptPath;
            fsc.textBoxUserPlayGetterPath.Text = Properties.Settings.Default.UserPlayGetterPath;
            if (fsc.ShowDialog() == DialogResult.OK)
            {
                Properties.Settings.Default.ChessEngineArguments = fsc.textBoxChessEngineArguments.Text;
                Properties.Settings.Default.ChessEnginePath = fsc.textBoxChessEnginePath.Text;
                Properties.Settings.Default.ComPort = fsc.textBoxComPort.Text;
                Properties.Settings.Default.MoveComputingHelperArgument = fsc.textBoxMoveComputingHelperArgument.Text;
                Properties.Settings.Default.MoveComputingHelperExe = fsc.textBoxMoveComputingHelperExe.Text;
                Properties.Settings.Default.MovePositionPythonScriptPath = fsc.textBoxMovePositionPythonScriptPath.Text;
                Properties.Settings.Default.UserPlayGetterPath = fsc.textBoxUserPlayGetterPath.Text;
            }
        }

        private void buttonInputFen_Click(object sender, EventArgs e)
        {
            if (chessMain == null)
            {
                MessageBox.Show("ゲーム開始前には設定できません。");
                return;
            }

            FormFenInput ffi = new FormFenInput();
            ffi.textBoxFen.Text = chessMain.Board.GetFEN();
            if (ffi.ShowDialog(this) == DialogResult.OK)
            {
                if (!string.IsNullOrEmpty(ffi.textBoxFen.Text))
                {
                    chessMain.Board.SetFen(ffi.textBoxFen.Text);
                    chessMain.Talker.SetBoardWithFEN(ffi.textBoxFen.Text);
                }
            }
        }

        private void timerGetLog_Tick(object sender, EventArgs e)
        {
            //チェス進行スレッドからのユーザー向けメッセージを表示する。
            lock (chessMain.MessageToGui)
            {
                if (chessMain.MessageToGui.Count > 0)
                {
                    textBoxGameProcess.AppendText(chessMain.MessageToGui.Dequeue() + Environment.NewLine);
                }
            }
        }

        private void timerGetMove_Tick(object sender, EventArgs e)
        {
            //チェス進行スレッドからの駒の移動を受け取り、段取りを構成する。
            PieceMoveToGui pmtg = null;
            lock (chessMain.OccuredMoveToGui)
            {
                if (chessMain.OccuredMoveToGui.Count > 0)
                {
                    pmtg = chessMain.OccuredMoveToGui.Dequeue();
                }
            }

            if (pmtg != null)
            {
                if (pmtg.MoveSAN != null)
                {
                    if (pmtg.IsWhiteMove)
                    {
                        ChessRecord.Append(string.Format("{0}. {1}", pmtg.FullMoveNumber, pmtg.MoveSAN));
                    }
                    else
                    {
                        ChessRecord.Append(string.Format(" {0}" + Environment.NewLine, pmtg.MoveSAN));
                    }

                    textBoxChessGameRecord.Text = ChessRecord.ToString();
                    textBoxChessGameRecord.Select(textBoxChessGameRecord.TextLength, 0);
                    textBoxChessGameRecord.ScrollToCaret();
                }
            }
        }

        private void timerAnimatePiece_Tick(object sender, EventArgs e)
        {
            //駒の移動を一定時間間隔でアニメーション表示するためのイベント。
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            //終了
            if (chessMain != null)
            {
                chessMain.ExitChessThread();
            }
            Properties.Settings.Default.Save();
        }
    }
}
