using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Net.Mime.MediaTypeNames;
using Newtonsoft;
using Newtonsoft.Json;
using System.Runtime.InteropServices;
using System.Runtime.ExceptionServices;
using System.Security.Cryptography.X509Certificates;

namespace paddleOCR
{
    public partial class Paddle : Form
    {
        public Paddle()
        {
            InitializeComponent();
            PaddleHelper.Init();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.Filter = "image files All files (*.*)|*.*";
                openFileDialog.RestoreDirectory = true;

                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    textBox1.Text = openFileDialog.FileName;
                }
            }
        }
        DateTime startTime;

        public static List<T> DeserializeJsonToList<T>(string json) where T : class
        {
            JsonSerializer serializer = new JsonSerializer();
            StringReader sr = new StringReader(json);
            object o = serializer.Deserialize(new JsonTextReader(sr), typeof(List<T>));
            List<T> list = o as List<T>;
            return list;
        }
        private void button3_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.InitialDirectory = "c:\\";
                openFileDialog.Filter = "python files (*.exe)|*.exe|All files (*.*)|*.*";
                openFileDialog.RestoreDirectory = true;

                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    textBox1.Text = openFileDialog.FileName;
                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                this.BeginInvoke(new Action(() =>
                {
                    try
                    {
                        listBox1.Items.Clear();
                        startTime = DateTime.Now;
                        string res = PaddleHelper.StartCPlusOcr(textBox1.Text);
                        listBox1.Items.AddRange(res.Split(';'));
                        this.label1.Text = $"elapsed:{(DateTime.Now - startTime).TotalSeconds.ToString("f2")}s";
                    }
                    catch
                    {
                    }
                }));
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message);
            }
        }
        private NotifyIcon notify;
        private ContextMenuStrip notifyMenu;
        bool exit=false;
        DeviceLibrary.Service service;
        string url = ConfigHelper.Config.Get("url", "http://localhost:8090");
        private void Form1_Load(object sender, EventArgs e)
        {
            service = new DeviceLibrary.Service();
            service.Open(url);
            //托盘菜单
            notifyMenu = new ContextMenuStrip();
            ToolStripMenuItem itemShow = new ToolStripMenuItem("Show(&S)");
            itemShow.Click += ItemShow_Click;
            ToolStripMenuItem itemExit = new ToolStripMenuItem("Exit(&X)");
            itemExit.Click += ItemExit_Click;
            notifyMenu.Items.Add(itemShow);
            notifyMenu.Items.Add(itemExit);
            //托盘控件
            notify = new NotifyIcon { Icon = Icon, Visible = true, ContextMenuStrip = notifyMenu, Text = Text };
           this.WindowState= FormWindowState.Minimized;
            ShowInTaskbar = false;
        }
        private void ItemShow_Click(object sender, EventArgs e)
        {
            Show();
            if (WindowState == FormWindowState.Minimized)
                WindowState = FormWindowState.Normal;
        }

        private void ItemExit_Click(object sender, EventArgs e)
        {
            notify.Dispose();
            exit = true;
            Close();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!exit)
            {
                e.Cancel = true;
                Hide();
            }
        }

        private void Paddle_FormClosed(object sender, FormClosedEventArgs e)
        {
            service.Close();
        }
    }
}