﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Interop;
using System.Xml.Serialization;

namespace TIASshot {
    public partial class Form1 : Form {

        public static string APP_NAME = "TIAS Shot";
        private Lucam _lucam;

        /// <summary>
        /// コンストラクタ
        /// </summary>
        public Form1() {
            InitializeComponent();

            _lucam = new Lucam(this, picPreview, picDisplay);

            var version = Assembly.GetExecutingAssembly().GetName().Version;
            Text = $"{APP_NAME} ver.{version.Major}.{version.Minor}";
#if DEBUG
            Text += " [DEBUG]";
#endif
        }

        /// <summary>
        /// フォームロード
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e) {
            if (!_lucam.Connect()) {
                MessageBox.Show(_lucam.ErrorMsg, APP_NAME,
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Close();
                return;
            }
            txtDeviceName.Text = _lucam.DeviceName;
            txtSerialNo.Text = _lucam.SerialNumber;
            txtSaveFolder.Text = Config.GetString("SaveFolder");
            EnableShots(false);
            _lucam.StartStopPreview();
        }

        /// <summary>
        /// フォームクローズ
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
            _lucam.Disconnect();
        }

        /// <summary>
        /// 画像撮影1枚
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnShot1_Click(object sender, EventArgs e) {
            _lucam.ShotOne();
        }

        /// <summary>
        /// 複数画像撮影
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnShotMulti_Click(object sender, EventArgs e) {
            _lucam.ShotMulti();
        }

        /// <summary>
        /// 校正
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCalib_Click(object sender, EventArgs e) {
            _lucam.Calibration();
        }

        /// <summary>
        /// 画像表示
        /// </summary>
        /// <param name="bmp"></param>
        public void ShowImage(Bitmap bmp) {
            //if (InvokeRequired) {
            //    Invoke((MethodInvoker)delegate { ShowImage(bmp); });
            //    return;
            //}
            picDisplay.Image = bmp;
        }

        /// <summary>
        /// メッセージ表示
        /// </summary>
        /// <param name="msg"></param>
        public void ShowMessage(string msg) {
            if (InvokeRequired) {
                Invoke((MethodInvoker)delegate { ShowMessage(msg); });
                return;
            }
            txtMessage.Text = msg;
        }

        /// <summary>
        /// 撮影ボタン有効化
        /// </summary>
        /// <param name="enable"></param>
        public void EnableShots(bool enable = true) {
            if (InvokeRequired) {
                Invoke((MethodInvoker)delegate { EnableShots(enable); });
                return;
            }
            btnShotOne.Enabled = enable;
            btnShotMulti.Enabled = enable;
        }
    }
}
