using Remontor.Connector;
using Remontor.Finder;
using Remontor.Words;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Remontor
{
    internal partial class MenuStripCommand : UserControl
    {
        public delegate void DUpdateTree();
        public DUpdateTree UpdateTree;
        public delegate void CloseMenuDelegate();
        event CloseMenuDelegate Close;

        private IComp Comp;
        public Form actForm;
        public Connect NowConnect = null;
        public MenuStripCommand()
        {
            InitializeComponent();
        }
        public void ShowMenu(IComp comp, Point point, CloseMenuDelegate close)
        {
            if(comp.GetIP() != null)
            {
                contextMenuCommand.Items[1].Enabled = true;
            }
            else
            {
                contextMenuCommand.Items[1].Enabled = false;
            }
            Comp = comp;

            contextMenuCommand.Items[7].Visible = true;
            contextMenuCommand.Items[8].Visible = true;
            contextMenuCommand.Show(point);
            contextMenuCommand.Closed+= ContextMenuCommand_Closed;
            Close = close;
            NowConnect = null;
        }

        public void ShowMenuUpdate(Connect connect, Point point, CloseMenuDelegate close)
        {
            ShowMenu(connect.GetComp, point, close);
            NowConnect = connect;
        }

        public void ShowMenuWords(IComp comp, Point point, CloseMenuDelegate close)
        {
            ShowMenu(comp, point, close);
            contextMenuCommand.Items[7].Visible = false;
            contextMenuCommand.Items[8].Visible = false;
        }

        private void ContextMenuCommand_Closed(object sender, ToolStripDropDownClosedEventArgs e)
        {
            if(Close != null) Close();    
        }

        private void CommandConnect_Click(object sender, EventArgs e)
        {
            if (NowConnect != null)
            {
                SConnector.ActionConnect.Reconnect();
            }
            else
            {
                if (actForm != null) actForm.Close();
                SConnector.NewConnect(Comp);
            }
            
        }

        private void CommandConnecIP_Click(object sender, EventArgs e)
        {
            if (NowConnect != null)
            {
                SConnector.ActionConnect.ReconnectIP();
            }
            else
            {
                if (actForm != null) actForm.Close();
                if (Comp.GetIP() != null) SConnector.NewConnectIP(Comp);
            }
           
        }

        private void CommandConnectRDP_Click(object sender, EventArgs e)
        {
            if (actForm != null) actForm.Close();
            Process.Start("mstsc.exe", "/v:" + Comp.GetNetNameStr());
        }

        private void CommandOpenFS_Click(object sender, EventArgs e)
        {
            if (actForm != null) actForm.Close();
            Process.Start("explorer", @"\\" + Comp.GetNetNameStr() + @"\c$");
        }

        private void CommandOpenRegEdit_Click(object sender, EventArgs e)
        {
            if (actForm != null) actForm.Close();
            Process.Start("sc", @"\\ComputerName  query RemoteRegistry" + Comp.GetNetNameStr());
        }

        private void CommandOpenControlPC_Click(object sender, EventArgs e)
        {
            if (actForm != null) actForm.Close();
            Process.Start("compmgmt.msc", @"/computer:" + Comp.GetNetNameStr());
        }

        private void CommandSavePC_Click(object sender, EventArgs e)
        {
            if (actForm != null) actForm.Close();
            BilderWords bilderWords = new BilderWords(Comp);
            DialogResult result = bilderWords.ShowDialog();
            if (result == DialogResult.OK)
            {
                UpdateTree();
            }
        }
    }
}
