using System;
using System.Collections.Generic;

namespace AlicizaX.UI
{
    public abstract class UGListBase<TData, TAdapter> where TAdapter : Adapter<TData> where TData : ISimpleViewData
    {
        protected readonly RecyclerView _recyclerView;

        protected readonly TAdapter _adapter;

        public RecyclerView RecyclerView => _recyclerView;

        public UGListBase(RecyclerView recyclerView, TAdapter adapter, Action<TData> onItemClick = null)
        {
            _recyclerView = recyclerView;
            _adapter = adapter;

            if (_recyclerView != null)
            {
                _recyclerView.SetAdapter(_adapter);
            }

            if (onItemClick != null)
            {
                _adapter.SetOnItemClick(onItemClick);
            }
        }

        public TAdapter Adapter => _adapter;

        private List<TData> _datas;

        public List<TData> Data
        {
            get => _datas;
            set
            {
                _datas = value;
                _adapter.SetList(_datas);
            }
        }
    }

    public class UGList<TData> : UGListBase<TData, Adapter<TData>> where TData : ISimpleViewData
    {
        public UGList(RecyclerView recyclerView, Action<TData> onItemClick = null)
            : base(recyclerView, new Adapter<TData>(recyclerView), onItemClick)
        {
        }
    }

    public class UGGroupList<TData> : UGListBase<TData, GroupAdapter<TData>> where TData : class, IGroupViewData, new()
    {
        public UGGroupList(RecyclerView recyclerView, string groupViewName, Action<TData> onItemClick = null)
            : base(recyclerView, new GroupAdapter<TData>(recyclerView, groupViewName), onItemClick)
        {
        }
    }

    public class UGLoopList<TData> : UGListBase<TData, LoopAdapter<TData>> where TData : ISimpleViewData, new()
    {
        public UGLoopList(RecyclerView recyclerView, Action<TData> onItemClick = null)
            : base(recyclerView, new LoopAdapter<TData>(recyclerView), onItemClick)
        {
        }
    }

    public class UGMixedList<TData> : UGListBase<TData, MixedAdapter<TData>> where TData : IMixedViewData
    {
        public UGMixedList(RecyclerView recyclerView, Action<TData> onItemClick = null)
            : base(recyclerView, new MixedAdapter<TData>(recyclerView), onItemClick)
        {
        }
    }

    public static class UGList
    {
        public static UGList<TData> Create<TData>(RecyclerView recyclerView, Action<TData> onItemClick = null) where TData : ISimpleViewData
            => new UGList<TData>(recyclerView, onItemClick);

        public static UGGroupList<TData> CreateGroup<TData>(RecyclerView recyclerView, string groupViewName, Action<TData> onItemClick = null) where TData : class, IGroupViewData, new()
            => new UGGroupList<TData>(recyclerView, groupViewName, onItemClick);

        public static UGLoopList<TData> CreateLoop<TData>(RecyclerView recyclerView, Action<TData> onItemClick = null) where TData : ISimpleViewData, new()
            => new UGLoopList<TData>(recyclerView, onItemClick);

        public static UGMixedList<TData> CreateMixed<TData>(RecyclerView recyclerView, Action<TData> onItemClick = null) where TData : IMixedViewData
            => new UGMixedList<TData>(recyclerView, onItemClick);
    }

}
