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

public class BulkOrderNotifyView : MonoBehaviour
{
    private static readonly int OpenTrigger = Animator.StringToHash("OpenTrigger");
    private static readonly int CloseTrigger = Animator.StringToHash("CloseTrigger");
    [SerializeField] private Animator backgroundAnimator;
    [SerializeField] private Button closeButton;
    
    [SerializeField] private Text messageText;
    [SerializeField] private BulkOrderNotifyItemView itemView1;
    [SerializeField] private BulkOrderNotifyItemView itemView2;
    [SerializeField] private BulkOrderNotifyItemView itemView3;
    [SerializeField] private Text coinText;
    [SerializeField] private Text heartText;
    [SerializeField] private BulkOrderCharacter bulkOrderCharacter;
    [SerializeField] private Image orderCharacterImage;

    private void Start()
    {
        if (LocalCacheManager.Load<BulkOrderData>(BulkOrder.DataTag, null) is BulkOrderData orderData)
        {
            var gameData = GameDataManager.GameData;
            var messageList = SpreadsheetDataManager.Instance.GetBaseDataList<BulkOrderTextData>(Const.BulkOrderTextDataSheet);
            messageText.text = messageList.First(data => data.id == gameData.OrderTextId).requestText;
            coinText.text = orderData.rewardCoin.ToString();
            heartText.text = orderData.rewardHeart.ToString();

            gameData.OrderIconIndex = bulkOrderCharacter.GetRandomIndex();
            GameDataManager.SaveGameData();
            orderCharacterImage.sprite = bulkOrderCharacter.GetNormalIcon(gameData.OrderIconIndex);
            
            var productList = SpreadsheetDataManager.Instance.GetBaseDataList<ProductData>(Const.ProductDataSheet);
            if (productList.FirstOrDefault(data => data.id == orderData.productId1) is ProductData data1)
            {
                itemView1.SetData(data1.name, orderData.count1, data1.GetIconPrefab());
                itemView1.SetActive(true);
            }
            else
            {
                itemView1.SetActive(false);
            }
            if (productList.FirstOrDefault(data => data.id == orderData.productId2) is ProductData data2)
            {
                itemView2.SetData(data2.name, orderData.count2, data2.GetIconPrefab());
                itemView2.SetActive(true);
            }
            else
            {
                itemView2.SetActive(false);
            }
            if (productList.FirstOrDefault(data => data.id == orderData.productId3) is ProductData data3)
            {
                itemView3.SetData(data3.name, orderData.count3, data3.GetIconPrefab());
                itemView3.SetActive(true);
            }
            else
            {
                itemView3.SetActive(false);
            }
        }
        closeButton.OnClickAsObservable().Take(1).Subscribe(_ =>
        {
            transform.parent.SetLocalScale(0);
            backgroundAnimator.SetTrigger(CloseTrigger);
            this.CallWaitForSeconds(.25f, () =>
            {
                TransitionManager.Instance.UnloadScene(GameScenes.BulkOrderTelephone);
                TransitionManager.Instance.LoadSceneAdditive(GameScenes.BulkOrder);
            });
        }).AddTo(this);
    }
}
