using System;
using System.Linq;
using UnityEngine;
using Random = UnityEngine.Random;

namespace MyGame.Scenes.marketing.Scripts
{
    public class CustomerSetting : MonoBehaviour
    {
        [SerializeField] private int specialOrderCount = 5;
        [SerializeField] private float walkerSpecialRate = .01f;
        [SerializeField] private float customerSpecialRate = .01f;
        
        private static readonly (int want, float weight)[] CustomerWeightTable = {
            (1, 88f),
            (2, 5f),
            (3, 1.9f),
            (4, 0.09f),
            (5, 0.01f),
            (0, 5f),
        };
        
        public (bool isSpecial, int orderCount) GetCustomerData (CustomerType customerType)
        {
            switch (customerType)
            {
                case CustomerType.Walker:
                    return Random.value < walkerSpecialRate ? (true, specialOrderCount) : (false, GetOrderCount());
                case CustomerType.Customer:
                    return Random.value < customerSpecialRate ? (true, specialOrderCount) : (false, GetOrderCount());
                case CustomerType.Vip:
                    return (true, specialOrderCount);
                default:
                    throw new ArgumentOutOfRangeException(nameof(customerType), customerType, null);
            }
        }
        
        // お客さん出現パターン確率計算と行動パターン計算
        private static int GetOrderCount()
        {
            var randomPoint = Random.value * CustomerWeightTable.Sum(x => x.weight);
            foreach (var value in CustomerWeightTable)
            {
                if (randomPoint < value.weight)
                { 
                    return value.want;
                }
                randomPoint -= value.weight;
            }
            return CustomerWeightTable.Last().want;
        }
    }
}