using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using Saltosion.OneWeapon.Effects;

namespace Saltosion.OneWeapon.Player {
    [RequireComponent(typeof(PlayerController))]
    public class PlayerFun : MonoBehaviour {

        private PlayerController Player;
        private CameraFX CameraFX;

        public float MaxFun = 100;
        public float FunDegradeSpeed = 5;
        public float StandingStillMultiplier = 1.5f;

        public float WeaponFun = 20;
        public float EnemyExplosionFun = 6f;
        public float FurnitureExplosionFun = 3f;
        public float ShootingFunAmount = 0.5f;

        public float CurrentFun { private set; get; }

        public bool DebugAdd10Fun = false;
        public bool DebugSubtract10Fun = false;
        public bool GodMode = false;
        public bool DebugAddKill = false;

        public float CurrentDamageBoost { private set; get; }

        public float KillStreakTimer = 2;
        public int KillStreak { private set; get; }
        public float CurrentKillStreakTime { private set; get; }

        void Start() {
            Player = GetComponent<PlayerController>();
            CameraFX = Camera.main.GetComponent<CameraFX>();
            CurrentFun = MaxFun * 0.75f;
        }

        void Update() {
            // God mode hotkey
            if (Input.GetButtonUp("GodModeToggle")) {
                GodMode = !GodMode;
            }

            float Delta = FunDegradeSpeed * Time.deltaTime;
            if (!Player.IsMoving) {
                Delta *= StandingStillMultiplier;
            }
            float NewFun = CurrentFun;
            if (!GodMode) {
                NewFun -= Delta;
            }

            if (DebugAdd10Fun) {
                NewFun += 10;
                DebugAdd10Fun = false;
            }
            if (DebugSubtract10Fun) {
                NewFun -= 10;
                DebugSubtract10Fun = false;
            }
            if (DebugAddKill) {
                GainKill();
                DebugAddKill = false;
            }

            CurrentFun = Mathf.Clamp(NewFun, 0, MaxFun);

            CurrentDamageBoost = Mathf.Floor((CurrentFun / 100) * 12) / 12;

            if (CurrentFun <= 0) {
                SceneManager.LoadScene(3);
            }

            // Kill Streak stuff
            CurrentKillStreakTime -= Time.deltaTime;
            if (CurrentKillStreakTime <= 0) {
                KillStreak = 0;
            }
        }

        private void AddFun(float fun) {
            float FunAdded = fun * (2 - (CurrentFun / MaxFun));
            float NewFun = CurrentFun + FunAdded;
            CurrentFun = Mathf.Min(MaxFun, NewFun);
        }

        public void TakeDamage() {
            if (GodMode) {
                return;
            }
            AddFun(-5f);
        }

        public void TakeExplosionDamage() {
            if (GodMode) {
                return;
            }
            AddFun(-10f);
        }

        public void ReceiveNewWeapon() {
            AddFun(WeaponFun);
        }

        public void ShootingFun() {
            AddFun(ShootingFunAmount);
        }

        public void Explosion(bool isFurniture) {
            float NewFun = CurrentFun;
            if (isFurniture) {
                AddFun(FurnitureExplosionFun);
            } else {
                AddFun(EnemyExplosionFun);
            }
        }

        public void GainKill() {
            KillStreak += 1;
            CurrentKillStreakTime = KillStreakTimer;
            if (KillStreak > 3) {
                CameraFX.SlowMotion(0.4f, Mathf.Min(KillStreak * 0.05f, 0.5f));
            }
        }
    }
}
