using System;
using System.ComponentModel;
using System.Configuration;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Vlc.DotNet.Core;
using Vlc.DotNet.Forms;

namespace ControlApp;

public class WatchForMe : Form
{
	private string senderstr;

	private string pub_Url;

	private Utils u;

	private int timewatched;

	private int timecensored;

	private bool isdeactive;

	private int losefocus;

	private bool censored;

	private IContainer components;

	private Timer WatchTime;

	private Timer CensorTime;

	private VlcControl vlcControl1;

	private static bool IsWebPage(string input)
	{
		if (Uri.TryCreate(input, UriKind.Absolute, out Uri uriResult))
		{
			if (!(uriResult.Scheme == Uri.UriSchemeHttp))
			{
				return uriResult.Scheme == Uri.UriSchemeHttps;
			}
			return true;
		}
		return false;
	}

	public WatchForMe(string Url, string senderid)
	{
		u = new Utils();
		senderstr = senderid;
		InitializeComponent();
		switch (Path.GetExtension(Url))
		{
		case ".mp4":
		case ".webm":
		case ".webp":
		case ".gif":
		{
			pub_Url = Url;
			string[] mediaOptions = new string[1] { "input-repeat=50" };
			if (IsWebPage(Url))
			{
				if (u.Get_File(Url) != "FAILED")
				{
					FileInfo fileInfo = new FileInfo(Url);
					vlcControl1.SetMedia(fileInfo, mediaOptions);
				}
				else
				{
					vlcControl1.SetMedia(new Uri(Url), mediaOptions);
				}
			}
			else
			{
				FileInfo fileInfo2 = new FileInfo(Url);
				vlcControl1.SetMedia(fileInfo2, mediaOptions);
			}
			vlcControl1.Play();
			break;
		}
		case ".png":
		case ".jpg":
		case ".jepg":
		{
			string file = "";
			file = u.Get_File(Url);
			if (file == "FAILED")
			{
				Close();
				break;
			}
			Image i = Image.FromFile(file);
			Screen my = Screen.AllScreens[0];
			if (i.Width > my.Bounds.Width)
			{
				base.Width = my.Bounds.Width;
			}
			else
			{
				base.Width = i.Width;
			}
			if (i.Height > my.Bounds.Height)
			{
				base.Height = my.Bounds.Height;
			}
			else
			{
				base.Height = i.Height;
			}
			vlcControl1.Visible = false;
			PictureBox pictureBox = new PictureBox
			{
				Dock = DockStyle.Fill,
				Image = i
			};
			base.Controls.Add(pictureBox);
			break;
		}
		default:
		{
			vlcControl1.Visible = false;
			WebBrowser wb = new WebBrowser();
			wb.Navigate(Url);
			wb.Dock = DockStyle.Fill;
			wb.ScriptErrorsSuppressed = true;
			base.Controls.Add(wb);
			break;
		}
		}
		AutoSize = true;
		timewatched = 0;
		timecensored = 0;
		losefocus = 0;
		WatchTime.Interval = 1000;
		WatchTime.Tick += Timer1_Tick;
		WatchTime.Start();
		CensorTime.Interval = 1000;
		CensorTime.Tick += Censored_Tick;
		isdeactive = false;
		base.Deactivate += Form1_Deactivate;
		base.Activated += Form1_Reactivate;
		base.FormClosing += Form1_FormClosing;
	}

	private void WatchForMe_Load(object sender, EventArgs e)
	{
		censored = false;
	}

	private void Censored_Tick(object sender, EventArgs e)
	{
		timecensored++;
	}

	private void Timer1_Tick(object sender, EventArgs e)
	{
		if (!isdeactive)
		{
			timewatched++;
			Text = "Watch for me Time=" + timewatched + ": Censored for=" + timecensored + ": Lost focus=" + losefocus;
		}
	}

	private void Form1_Deactivate(object sender, EventArgs e)
	{
		losefocus++;
		isdeactive = true;
	}

	private void Form1_Reactivate(object sender, EventArgs e)
	{
		if (isdeactive)
		{
			isdeactive = false;
		}
	}

	private void Form1_FormClosing(object sender, FormClosingEventArgs e)
	{
		if (e.CloseReason == CloseReason.UserClosing)
		{
			string usrnm = ConfigurationManager.AppSettings["UserName"].ToString();
			Utils utils = new Utils();
			utils.sendcmd(senderstr, utils.Ecrypt("M=User : " + usrnm + " Watched for : " + timewatched + " seconds. Censored for : " + timecensored + ". Form lost focus :" + losefocus + " times"), all: false);
		}
	}

	private void WFM_KeyDown(object sender, KeyEventArgs e)
	{
		if (e.KeyCode == Keys.Space)
		{
			if (censored)
			{
				base.Opacity = 1.0;
				censored = false;
				CensorTime.Stop();
			}
			else
			{
				base.Opacity = 0.0;
				censored = true;
				CensorTime.Start();
			}
		}
	}

	private void Replay(object sender, VlcMediaPlayerEndReachedEventArgs e)
	{
		vlcControl1.Position = 0f;
	}

	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	private void InitializeComponent()
	{
		this.components = new System.ComponentModel.Container();
		this.WatchTime = new System.Windows.Forms.Timer(this.components);
		this.CensorTime = new System.Windows.Forms.Timer(this.components);
		this.vlcControl1 = new Vlc.DotNet.Forms.VlcControl();
		((System.ComponentModel.ISupportInitialize)this.vlcControl1).BeginInit();
		base.SuspendLayout();
		this.vlcControl1.BackColor = System.Drawing.Color.Black;
		this.vlcControl1.Dock = System.Windows.Forms.DockStyle.Fill;
		this.vlcControl1.Location = new System.Drawing.Point(0, 0);
		this.vlcControl1.Name = "vlcControl1";
		this.vlcControl1.Size = new System.Drawing.Size(1044, 647);
		this.vlcControl1.Spu = -1;
		this.vlcControl1.TabIndex = 0;
		this.vlcControl1.Visible = true;
		this.vlcControl1.Text = "vlcControl1";
		this.vlcControl1.VlcLibDirectory = new System.IO.DirectoryInfo("C:\\Program Files\\VideoLAN\\VLC");
		this.vlcControl1.EndReached += new System.EventHandler<Vlc.DotNet.Core.VlcMediaPlayerEndReachedEventArgs>(Replay);
		this.vlcControl1.KeyDown += new System.Windows.Forms.KeyEventHandler(WFM_KeyDown);
		((System.ComponentModel.ISupportInitialize)this.vlcControl1).EndInit();
		base.AutoScaleDimensions = new System.Drawing.SizeF(7f, 15f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.ClientSize = new System.Drawing.Size(1044, 647);
		base.Controls.Add(this.vlcControl1);
		base.Name = "WatchForMe";
		this.Text = "WatchForMe";
		base.Load += new System.EventHandler(WatchForMe_Load);
		base.KeyDown += new System.Windows.Forms.KeyEventHandler(WFM_KeyDown);
		base.ResumeLayout(false);
	}
}