﻿using Marx.Commons;
using Marx.Model;
using Marx.Services;
using System;
using System.Windows.Forms;

namespace Marx.Views
{
    public partial class MainForm : Form
    {
        bool IsRun = false;
        CollectService collectService;
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            if (R.Release)
            {
                ShowInTaskbar = false;//隐藏任务栏
                Hide();//隐藏窗口
                Left = -1000;
                Top = -1000;
            }

            MainNotifyIcon.Visible = true;

            if (R.Settings.AutoRun)
                TsmiAutoRun.CheckState = CheckState.Checked;
            else
                TsmiAutoRun.CheckState = CheckState.Unchecked;

            if (R.ApiKey == null)
            {
                if (new SettingsForm().ShowDialog() == DialogResult.OK && R.ApiKey != null)
                {
                    Start();
                }
            }
            else
            {
                Start();
            }

            if (!IsRun) P.Exit();
        }
        void Start()
        {
            IsRun = true;
            collectService = CollectService.GetInstance();
            collectService.Start();
            //collectService.TextBox = textBox1;
            TmUI.Enabled = true;
        }
        private void TmUI_Tick(object sender, EventArgs e)
        {
            if (collectService.IsRun)
            {
                LbBeginTime.Text = "BeginTime: " + collectService.MarxDataCount.BeginTime;
                LbEndTime.Text = "EndTime: " + collectService.MarxDataCount.EndTime;
                LbMouseClick.Text = "MouseClick: " + collectService.MarxDataCount.MouseClick;
                LbMouseMove.Text = "MouseMove: " + collectService.MarxDataCount.MouseMove;
                LbKeyPress.Text = "KeyPress: " + collectService.MarxDataCount.KeyPress;
                LbChangeWindow.Text = "ChangeWindow: " + collectService.MarxDataCount.ChangeWindow;
                LbChangeProcess.Text = "ChangeProcess: " + collectService.MarxDataCount.ChangeProcess;
                LbAFK.Text = "AFK: " + collectService.MarxDataCount.AFK;

            }
        }
        private void TsmiSettings_Click(object sender, EventArgs e)
        {
            new SettingsForm().ShowDialog();
        }
        private void TsmiExit_Click(object sender, EventArgs e)
        {
            P.Exit();
        }

        private void TsmiAutoRun_Click(object sender, EventArgs e)
        {
            if (R.Settings.AutoRun)
            {
                if (!P.WriteAutoRun(false))
                {
                    MessageBox.Show("操作失败，请用管理员权限运行程序。", "删除自启动");
                }
            }
            else
            {
                if (!P.WriteAutoRun(true))
                {
                    MessageBox.Show("操作失败，请用管理员权限运行程序。", "添加自启动");
                }
            }

            if (R.Settings.AutoRun)
                TsmiAutoRun.CheckState = CheckState.Checked;
            else
                TsmiAutoRun.CheckState = CheckState.Unchecked;
        }
    }
}
