


using System;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using AppStudio.DataProviders;
using AppStudio.DataProviders.Core;
using AppStudio.DataProviders.Facebook;
using AppStudio.Uwp;
using AppStudio.Uwp.Actions;
using AppStudio.Uwp.Commands;
using AppStudio.Uwp.Navigation;
using NguyenAnNinhConfession.Config;
using NguyenAnNinhConfession.ViewModels;

namespace NguyenAnNinhConfession.Sections
{
    public class FacebookPostsConfig : SectionConfigBase<FacebookSchema>
    {
		private readonly FacebookDataProvider _dataProvider = new FacebookDataProvider(new FacebookOAuthTokens
        {
			AppId = "100121267049463",
                    AppSecret = "3a862d5c8694f5d0f3da339186b1d2bb"
        });

		public override Func<Task<IEnumerable<FacebookSchema>>> LoadDataAsyncFunc
        {
            get
            {
                var config = new FacebookDataConfig
                {
                    UserId = "494892910571287"
                };

                return () => _dataProvider.LoadDataAsync(config, MaxRecords);
            }
        }

        public override ListPageConfig<FacebookSchema> ListPage
        {
            get 
            {
                return new ListPageConfig<FacebookSchema>
                {
                    Title = "Facebook posts",

					PageTitle = "Facebook posts",

                    ListNavigationInfo = NavigationInfo.FromPage("FacebookPostsListPage"),

                    LayoutBindings = (viewModel, item) =>
                    {
                        viewModel.Title = item.Title.ToSafeString();
                        viewModel.SubTitle = item.PublishDate.ToString(DateTimeFormat.ShortDate);
                        viewModel.ImageUrl = ItemViewModel.LoadSafeUrl(item.ImageUrl.ToSafeString());
                    },
                    DetailNavigation = (item) =>
                    {
                        return NavigationInfo.FromPage("FacebookPostsDetailPage", true);
                    }
                };
            }
        }

        public override DetailPageConfig<FacebookSchema> DetailPage
        {
            get
            {
                var bindings = new List<Action<ItemViewModel, FacebookSchema>>();
                bindings.Add((viewModel, item) =>
                {
                    viewModel.PageTitle = item.Author.ToSafeString();
                    viewModel.Title = item.PublishDate.ToString(DateTimeFormat.ShortDate);
                    viewModel.Description = item.Content.ToSafeString();
                    viewModel.ImageUrl = ItemViewModel.LoadSafeUrl(item.ImageUrl.ToSafeString());
                    viewModel.Content = null;
					viewModel.Source = item.FeedUrl;
                });

                var actions = new List<ActionConfig<FacebookSchema>>
                {
                    ActionConfig<FacebookSchema>.Link("Go To Source", (item) => item.FeedUrl.ToSafeString()),
                };

                return new DetailPageConfig<FacebookSchema>
                {
                    Title = "Facebook posts",
                    LayoutBindings = bindings,
                    Actions = actions
                };
            }
        }
    }
}
