using JetBrains.Annotations;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEditor.VersionControl;
using UnityEngine;

[System.Serializable]
public class MeleeFighterClass : ClassBase {
  public MeleeFighterClass(Player player) : base(player) { }
  private readonly int BladeVortex = Animator.StringToHash("BladeVortex");
  private float BladeVortexSpeed = 0.4f;
  public float ComboResetTime;
  public float LastComboTime;
  public bool ChargingAnAttack;
  public float ChargeValue;
  public bool AllowBladeVortex;
  public int ChargeTick;
  public float TimeElapsed;
  public float Cooldown;
  public bool TimesUp;


  public AttackState CurrentState;
  public enum AttackState {
    None,
    PhaseCleave,
    DrainEdge,
    Shockwave,
    BladeVortex,
    BasicAttack1,
    BasicAttack2,
    BasicAttack3,
    KineticSurgeRelease,
    ChargeSurgeA,
    ChargeSurgeB,
    KineticSurgeA,
    KineticSurgeB,
  }


  override public void Tick() {
    TimeElapsed = Time.time - LastComboTime;
    TimesUp = TimeElapsed > ComboResetTime;

    switch (CurrentState) {
      case AttackState.None:
        Player.SkillInUse = false;
        return;

      case AttackState.BasicAttack1:
        AllowBladeVortex = TimeElapsed <= 0.1f;
        break;


        //case AttackState.ChargeSurgeA:
        //  ChargingAnAttack = Input.GetMouseButton(0);
        //  HandleCharging(TimeElapsed);
        //  if (!ChargingAnAttack) ChangeState(AttackState.KineticSurgeA);
        //  break;


        //case AttackState.ChargeSurgeB:
        //  ChargingAnAttack = Input.GetMouseButton(1);
        //  HandleCharging(TimeElapsed);
        //  if (!ChargingAnAttack) ChangeState(AttackState.KineticSurgeB);
        //  break;
    }

    if (CurrentState == AttackState.BasicAttack3 && TimeElapsed > 0.1) {
      ChangeState(AttackState.None);
      AttackAnimator.MeleeResetKineticCharge();
      AttackAnimator.MeleeKineticSurge();
      AnimationToPlay = "Attack1";
      TextPopUp.SpawnFloatingText("KineticSurge", Color.red, 3);
      Player.SkillInUse = false;
      Player.MoveSpeed = PlayerOriginalSpeed;
    }

    if (!ChargingAnAttack && TimesUp) {
      Player.MoveSpeed = PlayerOriginalSpeed;
      ChangeState(AttackState.None);
      AttackAnimator.MeleeResetKineticCharge();
      Player.SkillInUse = false;
    }
  }

  private void HandleCharging(float timeElapsed) {
    ChargeValue = timeElapsed * 60;
    if (ChargeValue > 100) ChargeValue = 100;
    if (ChargeTick++ > 10) {
      ChargeTick = 0;
      TextPopUp.SpawnFloatingText($"Charging {ChargeValue:0.0}", GetChargeColor(), 3);
    }
  }

  private Color GetChargeColor() {
    ChargeValue = Mathf.Clamp01(ChargeValue / 100f); // Normalize to 0–1

    if (ChargeValue < 0.5f) {
      return Color.Lerp(Color.red, Color.yellow, ChargeValue / 0.5f);
    } else {
      return Color.Lerp(Color.yellow, Color.green, (ChargeValue - 0.5f) / 0.5f);
    }
  }



  override public void HandleLMB() {
    Player.SkillInUse = true;

    switch (CurrentState) {
      case AttackState.None:
        PlayerOriginalSpeed = Player.MoveSpeed;

        if ((Time.time - Player.DashTime) <= 0.1f)
          ChangeState(AttackState.PhaseCleave, 1.5f);
        else if (Player.IsJumping)
          ChangeState(AttackState.Shockwave, 0, 1.5f);
        else
          ChangeState(AttackState.BasicAttack1, 0.25f);

        break;


      case AttackState.BasicAttack1:
        ChangeState(AttackState.BasicAttack2, 0.25f);
        break;


      case AttackState.BasicAttack2:
        ChangeState(AttackState.BasicAttack3, 0.25f);
        break;
    }
  }



  override public void HandleRMB() {
    Player.SkillInUse = true;

    switch (CurrentState) {
      case AttackState.None:
        PlayerOriginalSpeed = Player.MoveSpeed;

        if (Player.IsDashing)
          ChangeState(AttackState.None);
        else if (Player.IsJumping)
          ChangeState(AttackState.None);
        else
          ChangeState(AttackState.DrainEdge);
        break;


      case AttackState.BasicAttack1:
        if (!AllowBladeVortex) return;
        Cooldown = 0;
        ChangeState(AttackState.BladeVortex, 0.4f, 1.5f);
        break;


      case AttackState.BasicAttack3:
        //ChangeState(AttackState.ChargeSurgeB);
        break;
    }
  }


  private void ChangeState(
      AttackState state,
      float decreasedSpeed = 0.2f,
      float resetTime = 0.3f,
      float cooldown = 0.1f) {

    if (TimeElapsed < Cooldown) return;
    CurrentState = state;
    if (state == AttackState.None) return;
    Cooldown = cooldown;
    TextPopUp.SpawnFloatingText(state.ToString(), Color.red, 3);
    ComboResetTime = resetTime;
    LastComboTime = Time.time;
    Player.MoveSpeed = (state == AttackState.None) ? PlayerOriginalSpeed : PlayerOriginalSpeed * decreasedSpeed;

    switch (CurrentState) {
      case AttackState.None:
        if (Player.IsDashing) {
        } else if (Player.IsJumping) {
        } else {
        }
        break;

      case AttackState.Shockwave:
        break;

      case AttackState.BladeVortex:
        Animator.CrossFade(BladeVortex, 0);
        break;

      case AttackState.BasicAttack1:
        AnimationToPlay = "Attack1";
        AttackAnimator.MeleeBasic(1);
        break;

      case AttackState.BasicAttack2:
        AnimationToPlay = "Attack2";
        AttackAnimator.MeleeBasic(2);
        break;

      case AttackState.BasicAttack3:
        AnimationToPlay = "Attack1";
        AttackAnimator.MeleeBasic(3);
        break;

      case AttackState.KineticSurgeA:
        Debug.Log($"Charged Value: {ChargeValue}");
        break;

      case AttackState.KineticSurgeB:
        Debug.Log($"Charged Value: {ChargeValue}");
        break;
    }
  }
}
