using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PhilExample.UI.Views;
using Xamarin.Forms;

namespace PhilExample.UI.Pages
{
    public class PhExContentPage : ContentPage
    {
        protected override void OnAppearing()
        {
            base.OnAppearing();
            RecursiveCall(Content, (lcView) => lcView.OnAppearing);
        }

        protected override void OnDisappearing()
        {
            base.OnAppearing();
            RecursiveCall(Content, (lcView) => lcView.OnDisappearing);
        }

        private void RecursiveCall(View view, Func<ILifecycledView, Action> onLCViewFound)
        {
            if (view is ILifecycledView lcView)
            {
                onLCViewFound(lcView)();
            }
            else if (view is Layout<View> layoutT)
            {
                foreach (View child in layoutT.Children)
                    RecursiveCall(child, onLCViewFound);
            }
            else if (view is Layout layout)
            {
                foreach (Element child in layout.Children)
                    if (child is View v)
                        RecursiveCall(v, onLCViewFound);
            }
        }
    }
}
