﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Foundation;
using UIKit;

namespace Demo
{
    public class MainViewController:UIViewController
    {
        private ZYBannerView.ZYBannerView banner;

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            banner = new ZYBannerView.ZYBannerView();
            banner.ShouldLoop = true;
            banner.AutoScroll = true;
            banner.ScrollInterval = 3.0f;
            View.AddSubview(banner);
            banner.Frame = new CoreGraphics.CGRect(0, 0, View.Bounds.Width, 200);
            banner.Delegate = new ZYBannerDelegate();
            banner.DataSource = new ZYBannerDataSource(new List<UIImageView>()
            {
                new UIImageView(UIImageFromUrl(@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1493631420669&di=dddf293370b6e6fbdff7edd4ec2ab8a9&imgtype=0&src=http%3A%2F%2Fpic.qiantucdn.com%2F58pic%2F18%2F13%2F15%2F29Q58PIC4I6_1024.jpg")),
                new UIImageView(UIImageFromUrl(@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1493631463675&di=746dae2901a06bc46fb089b28187cbda&imgtype=0&src=http%3A%2F%2Fpic.58pic.com%2F58pic%2F17%2F14%2F25%2F56N58PICCmT_1024.jpg")),
                new UIImageView(UIImageFromUrl(@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1493631494337&di=1584c95119bd8cd8ad88f2b1e51ac974&imgtype=0&src=http%3A%2F%2Fpic.qiantucdn.com%2F58pic%2F18%2F24%2F75%2F78p58PICKbW_1024.jpg")),
            });
        }

        private UIImage UIImageFromUrl(string url)
        {
            var urlstr = url;
            using (var nsurl = new NSUrl(urlstr))
            {
                using (var data = NSData.FromUrl(nsurl))
                {
                    if (data != null)
                    {
                        return UIImage.LoadFromData(data);
                    }
                    else
                    {
                        return null;
                    }
                }
            }
        }
    }

    public class ZYBannerDelegate : ZYBannerView.ZYBannerViewDelegate
    {
        public override void Banner(ZYBannerView.ZYBannerView banner, nint index)
        {
            //base.Banner(banner, index);
        }

        public override void BannerFooterDidTrigger(ZYBannerView.ZYBannerView banner)
        {
            //base.BannerFooterDidTrigger(banner);
        }
    }

    public class ZYBannerDataSource : ZYBannerView.ZYBannerViewDataSource
    {
        private List<UIImageView> imageViews;

        public ZYBannerDataSource(List<UIImageView> imageViewList)
        {
            imageViews = imageViewList;
        }

        public override UIView Banner(ZYBannerView.ZYBannerView banner, nint index)
        {
            return imageViews[(int)index];
        }

        public override nint NumberOfItemsInBanner(ZYBannerView.ZYBannerView banner)
        {
            return imageViews.Count;
        }
    }
}