using System; using System.CodeDom.Compiler; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.IO; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Forms; using System.Windows.Markup; using System.Windows.Media; using HtmlAgilityPack; using Microsoft.Win32; using mshtml; namespace Gestor.Application.Componentes; public class WebEditor : UserControl, IComponentConnector { private ColorDialog ColorDialog; public static readonly DependencyProperty ImageVisibilityProperty = DependencyProperty.Register("ImageVisibility", typeof(Visibility), typeof(WebEditor), (PropertyMetadata)new FrameworkPropertyMetadata((object)(Visibility)2, (FrameworkPropertyMetadataOptions)256)); public static readonly DependencyProperty BarVisibilityProperty = DependencyProperty.Register("BarVisibility", typeof(Visibility), typeof(WebEditor), (PropertyMetadata)new FrameworkPropertyMetadata((object)(Visibility)0, (FrameworkPropertyMetadataOptions)256)); public static readonly DependencyProperty BodyProperty = DependencyProperty.Register("Body", typeof(string), typeof(WebEditor), (PropertyMetadata)new FrameworkPropertyMetadata((object)string.Empty, new PropertyChangedCallback(OnBoundBodyChanged))); public static readonly DependencyProperty IsReadOnlyProperty = DependencyProperty.Register("IsReadOnly", typeof(bool), typeof(WebEditor), (PropertyMetadata)new FrameworkPropertyMetadata((object)false, new PropertyChangedCallback(OnBoundReadOnlyChanged))); internal WrapPanel PanelControls; internal ComboBox ComboTextFormat; internal ComboBox ComboFontFamily; internal ComboBox ComboFontSize; internal Button SettingsBold; internal Button SettingsItalic; internal Button SettingsUnderLine; internal Button SettingsFontColor; internal Button SettingsBullets; internal Button SettingsNumbered; internal Button SettingsLeftAlign; internal Button SettingsCenter; internal Button SettingsRightAlign; internal Button SettingsJustifyFull; internal Button SettingsLink; internal TextBox ImagemLink; internal WebBrowser WebBrowserEditor; internal ItemsControl LinkPanel; internal TextBox HrefLink; internal CustomItemControl DescricaoBox; internal TextBox DescricaoLink; private bool _contentLoaded; public Visibility ImageVisibility { get { //IL_000b: Unknown result type (might be due to invalid IL or missing references) return (Visibility)((DependencyObject)this).GetValue(ImageVisibilityProperty); } set { //IL_0006: Unknown result type (might be due to invalid IL or missing references) ((DependencyObject)this).SetValue(ImageVisibilityProperty, (object)value); } } public Visibility BarVisibility { get { //IL_000b: Unknown result type (might be due to invalid IL or missing references) return (Visibility)((DependencyObject)this).GetValue(BarVisibilityProperty); } set { //IL_0006: Unknown result type (might be due to invalid IL or missing references) ((DependencyObject)this).SetValue(BarVisibilityProperty, (object)value); } } public string Body { get { return (string)((DependencyObject)this).GetValue(BodyProperty); } set { ((DependencyObject)this).SetValue(BodyProperty, (object)value); } } public bool IsReadOnly { get { return (bool)((DependencyObject)this).GetValue(IsReadOnlyProperty); } set { ((DependencyObject)this).SetValue(IsReadOnlyProperty, (object)value); } } public WebEditor() { //IL_000d: Unknown result type (might be due to invalid IL or missing references) //IL_0017: Expected O, but got Unknown InitializeComponent(); ColorDialog = new ColorDialog(); } private void SettingsBold_Click(object sender, RoutedEventArgs e) { WebBrowserEditor.HtmlDocument?.execCommand("Bold", showUI: false, Type.Missing); } private void SettingsItalic_Click(object sender, RoutedEventArgs e) { WebBrowserEditor.HtmlDocument?.execCommand("Italic", showUI: false, Type.Missing); } private void SettingsUnderLine_Click(object sender, RoutedEventArgs e) { WebBrowserEditor.HtmlDocument?.execCommand("Underline", showUI: false, Type.Missing); } private void SettingsRightAlign_Click(object sender, RoutedEventArgs e) { WebBrowserEditor.HtmlDocument?.execCommand("JustifyRight", showUI: false, Type.Missing); } private void SettingsLeftAlign_Click(object sender, RoutedEventArgs e) { WebBrowserEditor.HtmlDocument?.execCommand("JustifyLeft", showUI: false, Type.Missing); } private void SettingsCenter_Click(object sender, RoutedEventArgs e) { WebBrowserEditor.HtmlDocument?.execCommand("JustifyCenter", showUI: false, Type.Missing); } private void SettingsJustifyFull_Click(object sender, RoutedEventArgs e) { WebBrowserEditor.HtmlDocument?.execCommand("JustifyFull", showUI: false, Type.Missing); } private void SettingsInsertOrderedList_Click(object sender, RoutedEventArgs e) { WebBrowserEditor.HtmlDocument?.execCommand("InsertOrderedList", showUI: false, Type.Missing); } private void SettingsBullets_Click(object sender, RoutedEventArgs e) { WebBrowserEditor.HtmlDocument?.execCommand("InsertUnorderedList", showUI: false, Type.Missing); } public void InsertText(string textToInsert) { ((dynamic)WebBrowserEditor.HtmlDocument?.selection?.createRange())?.pasteHTML(textToInsert); } private void SettingsFontColor_Click(object sender, RoutedEventArgs e) { //IL_002f: Unknown result type (might be due to invalid IL or missing references) //IL_0034: Unknown result type (might be due to invalid IL or missing references) WebBrowserEditor.HtmlDocument = WebBrowserEditor.Browser.Document as HTMLDocument; if (WebBrowserEditor.HtmlDocument != null) { Color val = Pick(); string value = $"#{((Color)(ref val)).R:X2}{((Color)(ref val)).G:X2}{((Color)(ref val)).B:X2}"; WebBrowserEditor.HtmlDocument.execCommand("ForeColor", showUI: false, value); } } public Color Pick() { //IL_0002: Unknown result type (might be due to invalid IL or missing references) //IL_0026: Unknown result type (might be due to invalid IL or missing references) //IL_002c: Invalid comparison between Unknown and I4 //IL_0098: Unknown result type (might be due to invalid IL or missing references) //IL_002e: Unknown result type (might be due to invalid IL or missing references) Color result = default(Color); ColorDialog.AllowFullOpen = true; ColorDialog.FullOpen = true; if ((int)((CommonDialog)ColorDialog).ShowDialog() != 1) { return result; } ((Color)(ref result)).A = ColorDialog.Color.A; ((Color)(ref result)).B = ColorDialog.Color.B; ((Color)(ref result)).G = ColorDialog.Color.G; ((Color)(ref result)).R = ColorDialog.Color.R; return result; } private void ComboFontFamily_SelectionChanged(object sender, SelectionChangedEventArgs e) { WebBrowserEditor.HtmlDocument = WebBrowserEditor.Browser.Document as HTMLDocument; string value = ((Selector)ComboFontFamily).SelectedItem.ToString(); WebBrowserEditor.HtmlDocument?.execCommand("FontName", showUI: false, value); } private void ComboFontSize_SelectionChanged(object sender, SelectionChangedEventArgs e) { WebBrowserEditor.HtmlDocument = WebBrowserEditor.Browser.Document as HTMLDocument; WebBrowserEditor.HtmlDocument?.execCommand("FontSize", showUI: false, ((Selector)ComboFontSize).SelectedItem); } private void ComboTextFormat_SelectionChanged(object sender, SelectionChangedEventArgs e) { string value = ((Selector)ComboTextFormat).SelectedValue.ToString(); WebBrowserEditor.HtmlDocument = WebBrowserEditor.Browser.Document as HTMLDocument; WebBrowserEditor.HtmlDocument?.execCommand("FormatBlock", showUI: false, value); } private void Window_Loaded(object sender, RoutedEventArgs e) { if (WebBrowserEditor.Browser == null) { WebBrowserEditor.InstanciateNew(); } FontFamilyInitialize(); FontSizeInitialize(); FontFormatInitialize(); } private void FontFamilyInitialize() { ((ItemsControl)ComboFontFamily).ItemsSource = Fonts.SystemFontFamilies; ComboFontFamily.Text = "Times New Roman"; } private void FontFormatInitialize() { ((ItemsControl)ComboTextFormat).ItemsSource = new List> { new KeyValuePair("

", "Parágrafo"), new KeyValuePair("

", "Título 1"), new KeyValuePair("

", "Título 2"), new KeyValuePair("

", "Título 3"), new KeyValuePair("

", "Título 4"), new KeyValuePair("
", "Título 5"), new KeyValuePair("
", "Título 6"), new KeyValuePair("
", "Endereço"), new KeyValuePair("
", "Pré-formatação")
		};
		((Selector)ComboTextFormat).SelectedValuePath = "Key";
		((ItemsControl)ComboTextFormat).DisplayMemberPath = "Value";
		((Selector)ComboTextFormat).SelectedIndex = 0;
	}

	public string GetHtml()
	{
		string innerHTML = WebBrowserEditor.HtmlDocument.documentElement.innerHTML;
		innerHTML = innerHTML.Replace("about:", string.Empty);
		return HtmlDecode(innerHTML);
	}

	public string GetText()
	{
		if (!(WebBrowserEditor.HtmlDocument.documentElement.innerText.Trim() == "new"))
		{
			return WebBrowserEditor.HtmlDocument.documentElement.innerText;
		}
		return "";
	}

	public string HtmlDecode(string html)
	{
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		//IL_000b: Expected O, but got Unknown
		if (html == null)
		{
			return null;
		}
		HtmlDocument val = new HtmlDocument();
		val.LoadHtml(html);
		val.OptionWriteEmptyNodes = true;
		DecodeNode(val.DocumentNode);
		return val.DocumentNode.InnerHtml;
	}

	private void DecodeNode(HtmlNode node)
	{
		//IL_0055: Unknown result type (might be due to invalid IL or missing references)
		//IL_005b: Invalid comparison between Unknown and I4
		//IL_001e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0024: Invalid comparison between Unknown and I4
		if (node.HasChildNodes)
		{
			foreach (HtmlNode item in (IEnumerable)node.ChildNodes)
			{
				if ((int)item.NodeType == 3)
				{
					item.InnerHtml = WebUtility.HtmlDecode(item.InnerHtml);
				}
				else
				{
					DecodeNode(item);
				}
			}
			return;
		}
		if ((int)node.NodeType == 3)
		{
			node.InnerHtml = WebUtility.HtmlDecode(node.InnerHtml);
		}
	}

	public void Initialize(string htmlData = null)
	{
		WebBrowserEditor.InstanciateNew(htmlData);
	}

	private void FontSizeInitialize()
	{
		List list = new List();
		for (int i = 1; i <= 7; i++)
		{
			list.Add(i.ToString());
		}
		((ItemsControl)ComboFontSize).ItemsSource = list;
		ComboFontSize.Text = "3";
	}

	private void SettingsImage_Click(object sender, RoutedEventArgs e)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		//IL_0010: Unknown result type (might be due to invalid IL or missing references)
		//IL_001c: Expected O, but got Unknown
		OpenFileDialog val = new OpenFileDialog
		{
			Title = "Selecione a imagem",
			Filter = "Todos os formatos|*.jpg;*.jpeg;*.png|JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|Portable Network Graphic (*.png)|*.png"
		};
		if (((CommonDialog)val).ShowDialog().GetValueOrDefault())
		{
			string text = Path.GetExtension(((FileDialog)val).FileName).ToLower().Substring(1);
			string text2 = Convert.ToBase64String(File.ReadAllBytes(((FileDialog)val).FileName));
			string text3 = "data:image/" + text + ";base64," + text2;
			object obj = WebBrowserEditor.HtmlDocument?.selection?.createRange();
			string text4 = "\"Imagem\"";
			if (!string.IsNullOrEmpty(ImagemLink.Text.Trim()))
			{
				text4 = "" + text4 + "";
			}
			((dynamic)obj)?.pasteHTML(text4);
		}
	}

	private static void OnBoundBodyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
	{
		string htmlData = (string)((DependencyPropertyChangedEventArgs)(ref e)).NewValue;
		if (d != null && d is WebEditor webEditor)
		{
			webEditor.WebBrowserEditor.InstanciateNew(htmlData);
		}
	}

	private static void OnBoundReadOnlyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
	{
		bool flag = (bool)((DependencyPropertyChangedEventArgs)(ref e)).NewValue;
		if (d != null && d is WebEditor webEditor)
		{
			webEditor.WebBrowserEditor.ReadOnly(flag);
			((UIElement)webEditor.PanelControls).IsEnabled = !flag;
		}
	}

	private void SettingsLink_OnClick(object sender, RoutedEventArgs e)
	{
		HTMLDocument htmlDocument = WebBrowserEditor.HtmlDocument;
		((UIElement)DescricaoBox).Visibility = (Visibility)0;
		IHTMLSelectionObject selection = htmlDocument.selection;
		if (selection != null && selection.createRange() is IHTMLTxtRange iHTMLTxtRange)
		{
			((UIElement)DescricaoBox).Visibility = (Visibility)((!string.IsNullOrEmpty(iHTMLTxtRange.htmlText)) ? 2 : 0);
		}
		((UIElement)LinkPanel).Visibility = (Visibility)0;
		((UIElement)WebBrowserEditor).Visibility = (Visibility)1;
	}

	private void InsertLink_OnClick(object sender, RoutedEventArgs e)
	{
		if (string.IsNullOrWhiteSpace(HrefLink.Text))
		{
			return;
		}
		IHTMLSelectionObject selection = WebBrowserEditor.HtmlDocument.selection;
		if (selection != null && selection.createRange() is IHTMLTxtRange iHTMLTxtRange)
		{
			if (!string.IsNullOrEmpty(iHTMLTxtRange.htmlText))
			{
				iHTMLTxtRange.pasteHTML("" + iHTMLTxtRange.htmlText + "");
			}
			else
			{
				iHTMLTxtRange.pasteHTML("" + DescricaoLink.Text + "");
			}
		}
		((UIElement)LinkPanel).Visibility = (Visibility)2;
		((UIElement)WebBrowserEditor).Visibility = (Visibility)0;
	}

	private void CancelLink_OnClick(object sender, RoutedEventArgs e)
	{
		((UIElement)LinkPanel).Visibility = (Visibility)2;
		((UIElement)WebBrowserEditor).Visibility = (Visibility)0;
	}

	[DebuggerNonUserCode]
	[GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
	public void InitializeComponent()
	{
		if (!_contentLoaded)
		{
			_contentLoaded = true;
			Uri uri = new Uri("/Gestor.Application;component/componentes/webeditor.xaml", UriKind.Relative);
			Application.LoadComponent((object)this, uri);
		}
	}

	[DebuggerNonUserCode]
	[GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
	internal Delegate _CreateDelegate(Type delegateType, string handler)
	{
		return Delegate.CreateDelegate(delegateType, this, handler);
	}

	[DebuggerNonUserCode]
	[GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
	[EditorBrowsable(EditorBrowsableState.Never)]
	void IComponentConnector.Connect(int connectionId, object target)
	{
		//IL_007e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0088: Expected O, but got Unknown
		//IL_008b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0095: Expected O, but got Unknown
		//IL_0098: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a2: Expected O, but got Unknown
		//IL_00af: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b9: Expected O, but got Unknown
		//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c6: Expected O, but got Unknown
		//IL_00d3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00dd: Expected O, but got Unknown
		//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ea: Expected O, but got Unknown
		//IL_00f7: Unknown result type (might be due to invalid IL or missing references)
		//IL_0101: Expected O, but got Unknown
		//IL_0104: Unknown result type (might be due to invalid IL or missing references)
		//IL_010e: Expected O, but got Unknown
		//IL_011b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0125: Expected O, but got Unknown
		//IL_0128: Unknown result type (might be due to invalid IL or missing references)
		//IL_0132: Expected O, but got Unknown
		//IL_013f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0149: Expected O, but got Unknown
		//IL_014c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0156: Expected O, but got Unknown
		//IL_0163: Unknown result type (might be due to invalid IL or missing references)
		//IL_016d: Expected O, but got Unknown
		//IL_0170: Unknown result type (might be due to invalid IL or missing references)
		//IL_017a: Expected O, but got Unknown
		//IL_0187: Unknown result type (might be due to invalid IL or missing references)
		//IL_0191: Expected O, but got Unknown
		//IL_0194: Unknown result type (might be due to invalid IL or missing references)
		//IL_019e: Expected O, but got Unknown
		//IL_01ab: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b5: Expected O, but got Unknown
		//IL_01b8: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c2: Expected O, but got Unknown
		//IL_01cf: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d9: Expected O, but got Unknown
		//IL_01dc: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e6: Expected O, but got Unknown
		//IL_01f3: Unknown result type (might be due to invalid IL or missing references)
		//IL_01fd: Expected O, but got Unknown
		//IL_0200: Unknown result type (might be due to invalid IL or missing references)
		//IL_020a: Expected O, but got Unknown
		//IL_0217: Unknown result type (might be due to invalid IL or missing references)
		//IL_0221: Expected O, but got Unknown
		//IL_0224: Unknown result type (might be due to invalid IL or missing references)
		//IL_022e: Expected O, but got Unknown
		//IL_023b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0245: Expected O, but got Unknown
		//IL_0248: Unknown result type (might be due to invalid IL or missing references)
		//IL_0252: Expected O, but got Unknown
		//IL_025f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0269: Expected O, but got Unknown
		//IL_026c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0276: Expected O, but got Unknown
		//IL_0283: Unknown result type (might be due to invalid IL or missing references)
		//IL_028d: Expected O, but got Unknown
		//IL_0290: Unknown result type (might be due to invalid IL or missing references)
		//IL_029a: Expected O, but got Unknown
		//IL_029c: Unknown result type (might be due to invalid IL or missing references)
		//IL_02a8: Unknown result type (might be due to invalid IL or missing references)
		//IL_02b2: Expected O, but got Unknown
		//IL_02c2: Unknown result type (might be due to invalid IL or missing references)
		//IL_02cc: Expected O, but got Unknown
		//IL_02ce: Unknown result type (might be due to invalid IL or missing references)
		//IL_02da: Unknown result type (might be due to invalid IL or missing references)
		//IL_02e4: Expected O, but got Unknown
		//IL_02e7: Unknown result type (might be due to invalid IL or missing references)
		//IL_02f1: Expected O, but got Unknown
		//IL_0301: Unknown result type (might be due to invalid IL or missing references)
		//IL_030b: Expected O, but got Unknown
		//IL_030d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0319: Unknown result type (might be due to invalid IL or missing references)
		//IL_0323: Expected O, but got Unknown
		switch (connectionId)
		{
		case 1:
			((FrameworkElement)(WebEditor)target).Loaded += new RoutedEventHandler(Window_Loaded);
			break;
		case 2:
			PanelControls = (WrapPanel)target;
			break;
		case 3:
			ComboTextFormat = (ComboBox)target;
			((Selector)ComboTextFormat).SelectionChanged += new SelectionChangedEventHandler(ComboTextFormat_SelectionChanged);
			break;
		case 4:
			ComboFontFamily = (ComboBox)target;
			((Selector)ComboFontFamily).SelectionChanged += new SelectionChangedEventHandler(ComboFontFamily_SelectionChanged);
			break;
		case 5:
			ComboFontSize = (ComboBox)target;
			((Selector)ComboFontSize).SelectionChanged += new SelectionChangedEventHandler(ComboFontSize_SelectionChanged);
			break;
		case 6:
			SettingsBold = (Button)target;
			((ButtonBase)SettingsBold).Click += new RoutedEventHandler(SettingsBold_Click);
			break;
		case 7:
			SettingsItalic = (Button)target;
			((ButtonBase)SettingsItalic).Click += new RoutedEventHandler(SettingsItalic_Click);
			break;
		case 8:
			SettingsUnderLine = (Button)target;
			((ButtonBase)SettingsUnderLine).Click += new RoutedEventHandler(SettingsUnderLine_Click);
			break;
		case 9:
			SettingsFontColor = (Button)target;
			((ButtonBase)SettingsFontColor).Click += new RoutedEventHandler(SettingsFontColor_Click);
			break;
		case 10:
			SettingsBullets = (Button)target;
			((ButtonBase)SettingsBullets).Click += new RoutedEventHandler(SettingsBullets_Click);
			break;
		case 11:
			SettingsNumbered = (Button)target;
			((ButtonBase)SettingsNumbered).Click += new RoutedEventHandler(SettingsInsertOrderedList_Click);
			break;
		case 12:
			SettingsLeftAlign = (Button)target;
			((ButtonBase)SettingsLeftAlign).Click += new RoutedEventHandler(SettingsLeftAlign_Click);
			break;
		case 13:
			SettingsCenter = (Button)target;
			((ButtonBase)SettingsCenter).Click += new RoutedEventHandler(SettingsCenter_Click);
			break;
		case 14:
			SettingsRightAlign = (Button)target;
			((ButtonBase)SettingsRightAlign).Click += new RoutedEventHandler(SettingsRightAlign_Click);
			break;
		case 15:
			SettingsJustifyFull = (Button)target;
			((ButtonBase)SettingsJustifyFull).Click += new RoutedEventHandler(SettingsJustifyFull_Click);
			break;
		case 16:
			SettingsLink = (Button)target;
			((ButtonBase)SettingsLink).Click += new RoutedEventHandler(SettingsLink_OnClick);
			break;
		case 17:
			ImagemLink = (TextBox)target;
			break;
		case 18:
			((MenuItem)target).Click += new RoutedEventHandler(SettingsImage_Click);
			break;
		case 19:
			WebBrowserEditor = (WebBrowser)target;
			break;
		case 20:
			LinkPanel = (ItemsControl)target;
			break;
		case 21:
			((ButtonBase)(Button)target).Click += new RoutedEventHandler(CancelLink_OnClick);
			break;
		case 22:
			HrefLink = (TextBox)target;
			break;
		case 23:
			DescricaoBox = (CustomItemControl)target;
			break;
		case 24:
			DescricaoLink = (TextBox)target;
			break;
		case 25:
			((ButtonBase)(Button)target).Click += new RoutedEventHandler(InsertLink_OnClick);
			break;
		default:
			_contentLoaded = true;
			break;
		}
	}
}