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

public class DroppedWeapon : MonoBehaviour
{
    public GameObject weaponToGivePlayer;
    [System.NonSerialized] public Vector3 FloorPosition;
    [SerializeField] private Rarity rarity;
    private float rotationSpeed;
    [SerializeField] Transform weaponGraphic;
    [SerializeField] private float graphicHeightOffset = 0.7f;

    private GameObject effectParticle;
    private PooledObjectType type;
    private Vector3 originalPos;

    [SerializeField] private float scale = 1;

    [Space]

    [System.NonSerialized] public GameObject InfoCanvasGameObject;
    [System.NonSerialized] public WeaponInfoCanvas WeaponInfoCanvas;

    private void Awake()
    {
        originalPos = transform.position;

        Vector3 weaponGraphicPos = this.weaponGraphic.position;
        weaponGraphicPos.y -= graphicHeightOffset;
        this.weaponGraphic.position = weaponGraphicPos;

        gameObject.layer = LayerMask.NameToLayer("Default");
        gameObject.transform.localScale = new Vector3(scale, scale, scale);

        SetRarityParticle();

        InfoCanvasGameObject = PoolManager.Instance.PooledInstantiate(PooledObjectType.WeaponStatsCanvas, transform.position + new Vector3(0, 0.75f, 0), Quaternion.identity, null);
        WeaponInfoCanvas = InfoCanvasGameObject.GetComponent<WeaponInfoCanvas>();
        WeaponUpgradeConfig currentWeaponConfig = PlayerStats.Instance.weaponStats.weapon.UpgradeLevels[0];
        WeaponInfoCanvas.SetWeaponCanvasStats(currentWeaponConfig, weaponToGivePlayer.GetComponent<WeaponSystem>().weapon, rarity);
        InfoCanvasGameObject.SetActive(false);

        ResetMaterialState();
    }

    private void SetRarityParticle()
    {
        switch (rarity)
        {
            case Rarity.Common:
                this.type = CreateRarityParticle(PooledObjectType.Rarity_Common);
                rotationSpeed = 20;
                break;
            case Rarity.Rare:
                this.type = CreateRarityParticle(PooledObjectType.Rarity_Rare);
                rotationSpeed = 20;
                break;
            case Rarity.VeryRare:
                this.type = CreateRarityParticle(PooledObjectType.Rarity_VeryRare);
                rotationSpeed = 40;
                break;
            case Rarity.Epic:
                this.type = CreateRarityParticle(PooledObjectType.Rarity_Epic);
                rotationSpeed = 60;
                break;
            case Rarity.Legendary:
                this.type = CreateRarityParticle(PooledObjectType.Rarity_Legendary);
                rotationSpeed = 80;
                break;
        }
    }

    private void ResetMaterialState()
    {
        foreach (Renderer rend in transform.GetComponentsInChildren<Renderer>(true))
        {
            rend.material.SetInt("_ApplyFrozenEffect", 0);
        }
    }

    void Start()
    {
        if (Physics.Raycast(originalPos, Vector3.down, out RaycastHit hit, 10))
        {
            FloorPosition = new Vector3(hit.point.x, hit.point.y + 1.25f, hit.point.z);
        }
    }

    void Update()
    {
        //Sin wave weapons up and down and rotate them slowly

        if (hasParent)
        {
            transform.localPosition = new Vector3(0, Mathf.Sin(Time.time) * 0.1f, 0);
            transform.Rotate(0, rotationSpeed * Time.deltaTime, 0, Space.Self);
        }
        else
        {
            transform.position = new Vector3(originalPos.x, originalPos.y + (Mathf.Sin(Time.time) * 0.1f), originalPos.z);
            transform.Rotate(0, rotationSpeed * Time.deltaTime, 0);
        }

    }

    private bool hasParent;
    public void HasParent(bool state) => hasParent = state;

    private PooledObjectType CreateRarityParticle(PooledObjectType type)
    {
        effectParticle = PoolManager.Instance.PooledInstantiate(type, transform.position - new Vector3(0, 1.2f, 0), Quaternion.LookRotation(Vector3.up), null);
        effectParticle.transform.localScale = new Vector3(3f, 3f, 3f);

        return type;
    }

    public Transform GetRarityParticleObject() => effectParticle.transform;

    private void DestroyRarityParticle(PooledObjectType type)
    {
        PoolManager.Instance.PooledDestroy(type, effectParticle);
    }

    private void OnDestroy()
    {
        if (gameObject.scene.isLoaded)
        {
            DestroyRarityParticle(this.type);
        }
    }
}