using System;
using System.Collections.Generic;
using System.Linq;
using UniRx;
using UnityEngine;
using UnityEngine.UI;

public class MissionListView : MonoBehaviour
{
    [SerializeField] private MissionTabView normalTab;
    [SerializeField] private MissionTabView dailyTab;
    [SerializeField] private ScrollRect scrollRect;
    [SerializeField] private ScrollViewSlider scrollViewSlider;
    [SerializeField] private MissionView itemViewPrefab;
    [SerializeField] private MissionView dailyCompleteItemViewPrefab;

    private readonly ReactiveProperty<MissionCategory> selectedTab = new ReactiveProperty<MissionCategory>();
    public IReadOnlyReactiveProperty<MissionCategory> SelectedTab => selectedTab;
    private readonly List<(MissionCategory category, RectTransform content)> tabs = new List<(MissionCategory, RectTransform)>();

    private void Awake()
    {
        scrollRect.content.DestroyAllChildrens();
    }
    
    private void Start()
    {
        selectedTab.AddTo(this);
        selectedTab.Subscribe(tabName =>
        {
            // タブ表示切り替え
            normalTab.SetToggle(tabName == MissionCategory.Normal);
            dailyTab.SetToggle(tabName == MissionCategory.Daily);
            // コンテンツ切り替え
            tabs.ForEach(x =>
            {
                x.content.gameObject.SetActive(x.category == tabName);
                if (x.category == tabName)
                {
                    scrollRect.content = x.content;
                    LayoutRebuilder.ForceRebuildLayoutImmediate(scrollRect.content);
                    scrollRect.verticalNormalizedPosition = 1;
                }
            });
            scrollViewSlider.gameObject.SetActive(tabName == MissionCategory.Normal);
            scrollRect.vertical = tabName != MissionCategory.Daily;
        }).AddTo(this);
        normalTab.ClickObservable.Select(_ => MissionCategory.Normal)
            .Merge(dailyTab.ClickObservable.Select(_ => MissionCategory.Daily))
            .Subscribe(tabName =>
            {
                selectedTab.Value = tabName;
            }).AddTo(this);
    }

    public void ResetItemList(MissionCategory category)
    {
        var index = tabs.FindIndex(x => x.category == category);
        if (index == -1)
        {
            return;
        }
        tabs[index].content.DestroyAllChildrens();
    }

    public MissionView AddItemView(MissionData missionData)
    {
        var index = tabs.FindIndex(x => x.category == missionData.Category);
        if (index == -1)
        {
            index = tabs.Count;
            var content = scrollRect.content;
            tabs.Add((missionData.Category, Instantiate(content, content.parent)));
        }
        return Instantiate(itemViewPrefab, tabs[index].content);
    }

    public MissionView AddDailyCompleteItemView()
    {
        var index = tabs.FindIndex(x => x.category == MissionCategory.Daily);
        if (index == -1)
        {
            index = tabs.Count;
            var content = scrollRect.content;
            tabs.Add((MissionCategory.Daily, Instantiate(content, content.parent)));
        }
        return Instantiate(dailyCompleteItemViewPrefab, tabs[index].content);
    }

    public void SetTab(MissionCategory category)
    {
        selectedTab.SetValueAndForceNotify(category);
    }

    public void SetIndicator(MissionCategory category, bool active)
    {
        switch (category)
        {
            case MissionCategory.Normal:
                normalTab.SetIndicator(active);
                break;
            case MissionCategory.Daily:
                dailyTab.SetIndicator(active);
                break;
            default:
                throw new ArgumentOutOfRangeException(nameof(category), category, null);
        }
    }
}
