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

public enum ItemCategory
{
    Material = 1,
    Recipe = 2,
    Pan = 3,
    Special = 9999,
}

public enum ShopItemType
{
    Consumable = 1,
    NonConsumable = 2,
}

public enum ProductRarity
{
    Normal = 1,
    Yellow = 2,
    Copper = 3,
    Silver = 4,
    Gold = 5,
    Rainbow = 6,
}

public enum CornFieldUpgradeType
{
    FieldTop = 1,
    FieldCenter = 2,
    FieldBottom = 3,
    Machine = 4,
}

public enum ProductViewType
{
    Default = 1,
    BulkOrder = 2,
}

public enum MissionCategory
{
    Normal = 1,
    Daily = 2,
}

public enum MissionCondition
{
    CornCount = 1,
    CustomerCount = 2,
    SalesCount = 3,
    RareSalesCount = 4,
    CoinConsumeCount = 5,
    RecipeCount = 6,
    BulkOrderDeliveredCount = 7,
    SaltSalesCount = 8,
    RareSaltSalesCount = 9,
    CaramelSalesCount = 10,
    RareCaramelSalesCount = 11,
    PepperSalesCount = 12,
    RarePepperSalesCount = 13,
    HoneySalesCount = 14,
    RareHoneySalesCount = 15,
    CheeseSalesCount = 16,
    RareCheeseSalesCount = 17,
    SoysourceSalesCount = 18,
    RareSoysourceSalesCount = 19,
    StrawberrySalesCount = 20,
    RareStrawberrySalesCount = 21,
    GreenteaSalesCount = 22,              
    RareGreenteaSalesCount = 23,
    ChocolateSalesCount = 24,
    RareChocolateSalesCount = 25,
    NoriSaltSalesCount = 26,
    RareNoriSaltSalesCount = 27,
    ButterSoysourceSalesCount = 28,
    RareButterSoysourceSalesCount = 29,
    PepperCheeseSalesCount = 30,
    RarePepperCheeseSalesCount = 31,
    SaltCaramelSalesCount = 32,
    RareSaltCaramelSalesCount = 33,
    NoriButterSalesCount = 34,
    RareNoriButterSalesCount = 35,
    MilkChocolateSalesCount = 36,
    RareMilkChocolateSalesCount = 37,
    CaramelCheeseSalesCount = 38,
    RareCaramelCheeseSalesCount = 39,
    HoneyButterSalesCount = 40,
    RareHoneyButterSalesCount = 41,
    GreenteaMilkSalesCount = 42,
    RareGreenteaMilkSalesCount = 43,
    StrawberryChocolaSalesCount = 44,
    RareStrawberryChocolateSalesCount = 45,
    NoriSoysourceSalesCount = 46,
    RareNoriSoysourceSalesCount = 47,
    SaltButterSalesCount = 48,
    RareSaltButterSalesCount = 49,
    StrawberryMilkSalesCount = 50,
    RareStrawberryMilkSalesCount = 51,
    SaltChocolateSalesCount = 52,
    RareSaltChocolateSalesCount = 53,
    ChocolateGreenteaSalesCount = 54,
    RareChocolateGreenteaSalesCount = 55,
}

public enum MissionRewardType
{
    Coin = 1,
}

public enum BlueFaceType
{
    Normal = 1,
    CloseEye = 2,
    Happy = 3,
    Cheer = 4,
    Sweat = 5,
    Question = 6,
    Surprise = 7,
    Tired = 8,
    Smile = 9,
}

public enum PinkFaceType
{
    Normal = 1,
    Smile = 2,
    Happy = 3,
    Cheer = 4,
    Sweat = 5,
    Question = 6,
    Surprise = 7,
}

public enum BrotherType
{
    Blue = 1,
    Pink = 2,
    Both = 3,
}

[Serializable]
public sealed class MissionData
{
    public int id;
    public int category;
    public int shopLevel;
    public int condition;
    public int count;
    public int rewardType;
    public int reward;
    public string text;
    public MissionCategory Category => (MissionCategory) category;
    public MissionCondition Condition => (MissionCondition) condition;
    public MissionRewardType RewardType => (MissionRewardType) rewardType;
}

[Serializable]
public sealed class RarityData
{
    public int id;
    public int bulkOrderBonus;
    public string text;
    public ProductRarity Rarity => (ProductRarity)id;
}

[Serializable]
public sealed class PanData
{
    public int id;
    public int nameId;
    public string name;
    public string text;
    public int rarity;
    public string prefabName;
    public float panFactor;
    public float deadZone;
    public float baseIncreaseSpeed;
    public float coldIncreaseSpeed;
    public float hotIncreaseSpeed;
    public float maxDecreaseSpeed;
    public float yellowSize;
    public float randomRange;
    public ProductRarity Rarity => (ProductRarity)rarity;
    public Transform GetPrefab()
    {
        return Resources.Load<Transform>("Pans/" + prefabName);
    }
    public RectTransform GetIconPrefab()
    {
        return Resources.Load<RectTransform>("MaterialIcons/" + prefabName);
    }
}

[Serializable]
public sealed class FieldUpgradeData
{
    public int type;
    public int level;
    public int price;
    public int harvested;
    public string prefabName;
    public CornFieldUpgradeType Type => (CornFieldUpgradeType) type;

    public RectTransform GetIconPrefab()
    {
        return Resources.Load<RectTransform>("MachineIcons/" + prefabName);
    }

    public Animator GetPrefab()
    {
        return Resources.Load<Animator>("Machines/" + prefabName);
    }
    public static CornFieldUpgradeType PlantTypeToUpgradeType(PlantLineType lineType)
    {
        switch (lineType)
        {
            case PlantLineType.Top:
                return CornFieldUpgradeType.FieldTop;
            case PlantLineType.Center:
                return CornFieldUpgradeType.FieldCenter;
            case PlantLineType.Bottom:
                return CornFieldUpgradeType.FieldBottom;
            default:
                throw new ArgumentOutOfRangeException(nameof(lineType), lineType, null);
        }
    }
}

[Serializable]
public sealed class ProductData 
{
    public int id;
    public int nameId;
    public string name;
    public int shopLevel;
    public int price;
    public int volume;
    public string text;
    public int material1Id;
    public int material2Id;
    public int material3Id;
    public int material1Count;
    public int rarity1Price;
    public int rarity2Price;
    public int rarity3Price;
    public int rarity4Price;
    public int rarity5Price;
    public int rarity6Price;
    // public int cookingType;
    public string prefabName;
    private List<(int id, int amount)> materialList;
    public List<(int id, int amount)> MaterialList
    {
        get
        {
            if (materialList == null)
            {
                ChangeAmount(1);
            }
            return materialList;
        }
    }

    public ProductData(){}
    public ProductData(ProductData productData, int value)
    {
        id = productData.id;
        nameId = productData.nameId;
        name = productData.name;
        shopLevel = productData.shopLevel;
        price = productData.price;
        volume = productData.volume;
        text = productData.text;
        material1Id = productData.material1Id;
        material2Id = productData.material2Id;
        material3Id = productData.material3Id;
        material1Count = productData.material1Count;
        prefabName = productData.prefabName;
        ChangeAmount(value);
    }

    private void ChangeAmount(int value)
    {
        materialList = new List<(int id, int amount)>
        {
            (material1Id, material1Count * value),
            (material2Id, 1 * value),
            (material3Id, 1 * value),
        };
    }

    public int GetMaterialCount()
    {
        return materialList.Count(material => material.id != Const.NotSetMaterialId);
    }

    public int GetRarityPrice(ProductRarity rarity)
    {
        switch (rarity)
        {
            case ProductRarity.Normal:
                return rarity1Price;
            case ProductRarity.Yellow:
                return rarity2Price;
            case ProductRarity.Copper:
                return rarity3Price;
            case ProductRarity.Silver:
                return rarity4Price;
            case ProductRarity.Gold:
                return rarity5Price;
            case ProductRarity.Rainbow:
                return rarity6Price;
            default:
                throw new ArgumentOutOfRangeException(nameof(rarity), rarity, null);
        }
    }
    
    public Transform GetIconPrefab()
    {
        return Resources.Load<Transform>("PopcornIcons/" + (string.IsNullOrEmpty(prefabName) ? "package_salt" : prefabName));
    }
    public Transform GetLargePrefab()
    {
        return Resources.Load<Transform>("PopcornLargeIcons/" + (string.IsNullOrEmpty(prefabName) ? "package_salt" : prefabName));
    }
    
    public MarketPopcornView GetMarketPrefab()
    {
        return Resources.Load<MarketPopcornView>("Popcorns/" + (string.IsNullOrEmpty(prefabName) ? "package_salt" : prefabName));
    }
}

[Serializable]
public sealed class BulkOrderData
{
    public int id;
    public int shopLevel;
    public int maxOrder;
    public int firstOrderCondition;
    public int retryCondition;
    public int productId1;
    public int count1;
    public int productId2;
    public int count2;
    public int productId3;
    public int count3;
    public int rewardCoin;
    public int rewardHeart;
}

[Serializable]
public sealed class BulkOrderTextData
{
    public int id;
    public int shopLevel;
    public string requestText;
    public string thanksText;
}

[Serializable]
public sealed class ShopLevelData
{
    public int id;
    public int shopLevel;
    public int heart;
    public int customer;
    public string achievement;
}

[Serializable]
public sealed class BrotherScriptData
{
    public int id;
    public string text;
    public int brother;
    public int blueFace;
    public int pinkFace;
    public BrotherType Brother => (BrotherType) brother;
    public BlueFaceType BlueFace => (BlueFaceType) blueFace;
    public PinkFaceType PinkFace => (PinkFaceType) pinkFace;
}

[Serializable]
public sealed class ShopData {
    public int id;
    public int nameId;
    public int itemId;
    public string name;
    public int price;
    public string text;
    public int category;
    public int consumeType;
    public string prefabName;
    public ItemCategory Category => (ItemCategory)category;
    public ShopItemType ConsumeType => (ShopItemType)consumeType;

    public Transform GetPrefab()
    {
        return Resources.Load<Transform>("Materials/" + (string.IsNullOrEmpty(prefabName) ? "salt" : prefabName));
    }
    public Transform GetIconPrefab()
    {
        return Resources.Load<Transform>("MaterialIcons/" + (string.IsNullOrEmpty(prefabName) ? "corn" : prefabName));
    }
}