using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telerik.XamarinForms.Primitives;
using Xamarin.Forms;

namespace DynamicTabManagement.Portable
{
    public partial class StartPage : ContentPage
    {
        private ObservableCollection<TabViewItem> RemovedTabs { get; set; } = new ObservableCollection<TabViewItem>();

        public StartPage()
        {
            InitializeComponent();
        }

        private void Remove_OnClicked(object sender, EventArgs e)
        {
            // Find the tab you want to remove
            var folderTab = tabView.Items.FirstOrDefault(t => t.HeaderText == "Folder");

            // place it in the cache 
            if(!RemovedTabs.Contains(folderTab)) RemovedTabs.Add(folderTab);

            // remove it from the TabView
            if(tabView.Items.Contains(folderTab)) tabView.Items.Remove(folderTab);
        }

        private void Add_OnClicked(object sender, EventArgs e)
        {
            // Check the cache
            var folderTab = RemovedTabs.FirstOrDefault(t => t.HeaderText == "Folder");
            
            if (folderTab != null)
            {
                // If the tab was in the cache, go ahead and add it..

                tabView.Items.Insert(1, folderTab); // Use Insert() to re-add at a specific index
                // tabView.Items.Add(folderTab); Use Add to add at the end of the collection
                

                // Finally, remove it from the cache
                if(RemovedTabs.Contains(folderTab)) RemovedTabs.Remove(folderTab);
            }
            else
            {
                // If the tab doesn't exist in the cache, create a new one
                tabView.Items.Add(new TabViewItem {HeaderText = "Folder"});
            }
        }
    }
}

public partial class StartPage : ContentPage
{
    private ObservableCollection<TabViewItem> RemovedTabs { get; set; } = new ObservableCollection<TabViewItem>();

    public StartPage()
    {
        InitializeComponent();
    }

    private void Remove_OnClicked(object sender, EventArgs e)
    {
        // Find the tab you want to remove
        var folderTab = tabView.Items.FirstOrDefault(t => t.HeaderText == "Folder");

        // place it in the cache 
        if(!RemovedTabs.Contains(folderTab)) RemovedTabs.Add(folderTab);

        // remove it from the TabView
        if(tabView.Items.Contains(folderTab)) tabView.Items.Remove(folderTab);
    }

    private void Add_OnClicked(object sender, EventArgs e)
    {
        // Check the cache
        var folderTab = RemovedTabs.FirstOrDefault(t => t.HeaderText == "Folder");
            
        if (folderTab != null)
        {
            // If the tab was in the cache, go ahead and add it..

            tabView.Items.Insert(1, folderTab); // Use Insert() to re-add at a specific index
            // tabView.Items.Add(folderTab); Use Add to add at the end of the collection
                

            // Finally, remove it from the cache
            if(RemovedTabs.Contains(folderTab)) RemovedTabs.Remove(folderTab);
        }
        else
        {
            // If the tab doesn't exist in the cache, create a new one
            tabView.Items.Add(new TabViewItem {HeaderText = "Folder"});
        }
    }
}