using Billing.iOS.Renderers;
using Billing.UI;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ExportRenderer(typeof(BillingPage), typeof(BillingPageRenderer))]
namespace Billing.iOS.Renderers
{
    public class BillingPageRenderer : PageRenderer
    {
        /* Custom navigation left button, but has an issue
        public override void ViewWillAppear(bool animated)
        {
            base.ViewWillAppear(animated);

            var navigationItem = NavigationController?.TopViewController.NavigationItem;
            if (navigationItem == null)
            {
                return;
            }

            if (Element is BillingPage page)
            {
                var itemsInfo = page.ToolbarItems;
                var leftButtons = navigationItem.LeftBarButtonItems?.ToList() ?? new();
                var rightButtons = navigationItem.RightBarButtonItems?.ToList() ?? new();

                for (var i = rightButtons.Count - 1; i >= 0; i--)
                {
                    var item = rightButtons[i];
                    var info = itemsInfo[i];
                    if (info == null)
                    {
                        continue;
                    }
                    if (info.Priority == 1)
                    {
                        item.Style = UIBarButtonItemStyle.Done;
                    }
                    else if (info.Priority == -1)
                    {
                        rightButtons.Remove(item);
                        leftButtons.Add(item);
                    }
                }

                navigationItem.RightBarButtonItems = rightButtons.ToArray();
                navigationItem.LeftBarButtonItems = leftButtons.ToArray();
            }
        }
        //*/

        public override void ViewDidAppear(bool animated)
        {
            base.ViewDidAppear(animated);
            if (Element is BillingPage page)
            {
                page.TriggerLoad();
            }
        }
    }
}
