using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using Saltosion.OneWeapon.Player;
using Saltosion.OneWeapon.Guns;
using Saltosion.OneWeapon.Utils;

namespace Saltosion.OneWeapon.GUI {
    public class HUDController : MonoBehaviour {


        [Header("General")]
        public PlayerController Player;
        public PlayerFun PlayerFun;
        public RawImage FunMeter;
        public Image GunImage;

        [Header("Gore Texts")]
        public TMP_Text DamageTextGore;
        public TMP_Text FunTextGore;
        public TMP_Text GunTextGore;

        [Header("Fun Texts")]
        public TMP_Text DamageTextFun;
        public TMP_Text FunTextFun;
        public TMP_Text GunTextFun;

        [Header("Kill Streak stuff")]
        public GameObject KillStreakTransform;
        public TMP_Text KSNumberGore;
        public TMP_Text KSNumberFun;
        public RawImage StreakMeter;

        private string[] GoreTexts = new string[] { "I'm gonna die of boredom!", "Ultra lame!", "Oof, boring!", "That's cool!", "Holy fuck!", "Bloody awesome!" };
        private string[] FunTexts = new string[] { "I'm gonna fall asleep!", "You should try doing something!", "There aren't enough flowers", "Now that's pretty!", "You're doing great!", "Faboulous!" };

        private Gun LastGun;

        private int LastKillCount = 2;
        private float CurrentKSRot;
        private float RotMultiplier;
        private bool RotRising = false;

        void Start() {
            GunTextGore.text = "";
            GunTextFun.text = "";
        }

        void Update() {
            /// Fun Bar
            float FunPercent = (PlayerFun.CurrentFun / PlayerFun.MaxFun);
            Vector3 Scale = FunMeter.transform.localScale;

            Scale.x = FunPercent;
            FunMeter.transform.localScale = Scale;
            Rect UVRect = FunMeter.uvRect;
            UVRect.width = FunPercent;
            FunMeter.uvRect = UVRect;

            float FunLevel = Mathf.Floor(FunPercent * 6) / 6;

            Color Color = FunMeter.color;
            Color.g = FunLevel;
            Color.r = 1 - FunLevel;
            FunMeter.color = Color;

            int FunPercentNumber = (int)Mathf.Round(PlayerFun.CurrentDamageBoost * 100);
            string Text = "+" + FunPercentNumber + "%";
            DamageTextGore.text = Text;
            DamageTextFun.text = Text;

            Color GoreColor = DamageTextGore.color;
            GoreColor.g = 1 - PlayerFun.CurrentDamageBoost;
            GoreColor.b = 1 - PlayerFun.CurrentDamageBoost;
            DamageTextGore.color = GoreColor;
            FunTextGore.color = GoreColor;

            Color FunColor = DamageTextFun.color;
            FunColor.b = 1 - PlayerFun.CurrentDamageBoost;
            DamageTextFun.color = FunColor;
            FunTextFun.color = FunColor;

            int FunLevelInt = (int)Mathf.Clamp(Mathf.Floor(FunPercent * 6), 0, 5);
            FunTextGore.text = GoreTexts[FunLevelInt];
            FunTextFun.text = FunTexts[FunLevelInt];

            /// Gun HUD
            if (Player.Gun != LastGun) {
                LastGun = Player.Gun;
                if (LastGun == null) {
                    GunTextGore.text = "";
                    GunTextFun.text = "";
                    GunImage.color = new Color(0, 0, 0, 0);
                } else {
                    GunTextGore.text = LastGun.Name;
                    GunTextFun.text = LastGun.Name;
                    GunImage.sprite = LastGun.GunSprite;
                    GunImage.color = new Color(1, 1, 1, 1);
                }
            }

            /// Kill Streak
            if (PlayerFun.KillStreak < 2) {
                KillStreakTransform.SetActive(false);
            } else {
                KillStreakTransform.SetActive(true);
                string Number = PlayerFun.KillStreak.ToString();
                KSNumberFun.text = Number;
                KSNumberGore.text = Number;

                if (RotRising) {
                    CurrentKSRot += Time.deltaTime * 500;
                    if (CurrentKSRot >= 20) {
                        RotRising = false;
                    }
                } else {
                    CurrentKSRot -= Time.deltaTime * 500;
                    if (CurrentKSRot <= -20) {
                        RotRising = true;
                    }
                }
                RotMultiplier -= Time.deltaTime * 6;
                if (RotMultiplier <= 0) {
                    RotMultiplier = 0;
                    CurrentKSRot = 0;
                }
                Vector3 Rot = KSNumberGore.transform.localEulerAngles;
                Rot.z = RotMultiplier * CurrentKSRot;
                KSNumberGore.transform.localEulerAngles = Rot;
                KSNumberFun.transform.localEulerAngles = Rot;

                float StreakPercent = (PlayerFun.CurrentKillStreakTime / PlayerFun.KillStreakTimer);
                Scale = StreakMeter.transform.localScale;
                Scale.x = StreakPercent;
                StreakMeter.transform.localScale = Scale;

                GoreColor = StreakMeter.color;
                FunColor = StreakMeter.color;

                GoreColor.r = 1;
                GoreColor.g = 1 - StreakPercent;
                GoreColor.b = 1 - StreakPercent;

                FunColor.r = 1;
                FunColor.g = 1;
                FunColor.b = 1 - StreakPercent;

                if (Options.CensorGore) {
                    StreakMeter.color = FunColor;
                } else {
                    StreakMeter.color = GoreColor;
                }

                int delta = PlayerFun.KillStreak - LastKillCount;
                if (delta > 0) {
                    RotMultiplier += 1f * delta;
                }

                LastKillCount = PlayerFun.KillStreak;
            }
        }
    }
}
