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

public class MoveCharacter : Character
{
    public Foot foot;

    public float extraRiseGravity = 0; //上升时额外重力加速度
    public float extraFallGravity = -10; //下落时额外重力加速度
    public float moveSpeed = 5;
    //[HideInInspector]
    public float beRepelValue;
    public float totalBeRepelValue;
    [HideInInspector]
    public float weakTime;
    public float totalWeakTime;
    public float weakUpSpeed = 10f;
    public float decelerationRatio = 1f;
    public float minHurtKeepTime = 0.2f;
    [HideInInspector]
    public float hurtKeepTime = 0;
    public float hurtChangeVelocity = 1;

    [Header("新增漂浮效果参数")]
    public float maxTime = 15; //上升最大耗时
    public float minTime = 6;  //上升最小耗时
    public float maxHeight = 12;    //最大上升高度
    public float minHeight = 7;    //最小上升高度

    public float maxRotateSpeed;    //最大旋转速度
    public float minRotateSpeed;    //最小旋转速度

    public float floatTime = 20;     //漂浮时间

    private float curTime;      //漂浮已进行时长
    private float height;       //漂浮高度
    private float riseTime;   //上升时间
    private float curHeight;    //当前所在高度
    private float rotateSpeed;  //旋转速度
    private float rotateDir;    //旋转方向

    private Vector3 origPos;     //初始位置
    private float rise = 1;
    private float down = -1;
    private Vector3 oneClockwise = new Vector3(0, 0, 1);
    private Vector3 oneCounterClockwise = new Vector3(0, 0, -1);

    private int floatState; //0:不漂浮；1:漂浮中；2:飘着；3:掉下去



    public void FloatStateOn()
    {
        maxTime = 1.5f; 
        minTime = 0.1f;
        maxHeight = 9;
        minHeight = 4;

        floatTime = 20;     
        ChangeState(CharacterState.Rise);
        floatState = 1;
        riseTime = Random.Range(minTime, maxTime);
        origPos = transform.position;
        curHeight = origPos.y;
        curTime = 0;
        rotateSpeed = Random.Range(minRotateSpeed, maxRotateSpeed);
        rotateDir = (1.5f - Random.Range(1, 3)) * 2;
        height = Random.Range(minHeight, maxHeight);
    }

    private void RotateSelf()
    {
        //transform.localEulerAngles += new Vector3(0, 0, 1) * rotateDir * rotateSpeed * Time.deltaTime;
    }


    private void Update()
    {
        if (floatState == 1)
        {
            RotateSelf();
            curTime += Time.deltaTime;
            aniCollider.Play("Rise", 0, 0);
            curHeight = Mathf.SmoothDamp(curHeight, height, ref rise, riseTime);
            transform.position = new Vector3(origPos.x, curHeight, origPos.z);
            if (curHeight >= height - 0.02f)
            {
                floatState = 2;
                ChangeState(CharacterState.Float);
                transform.position = new Vector3(origPos.x, height, origPos.z);
            }
        }
        else if (floatState == 2)
        {
            RotateSelf();
            curTime += Time.deltaTime;
            transform.position = new Vector3(origPos.x, height, origPos.z);
            if (curTime >= floatTime)
            {
                floatState = 3;
                ChangeState(CharacterState.Fall);
            }
        }
        else if (floatState == 3)
        {
            RotateSelf();
            aniCollider.Play("Fall", 0, 0);

            if (transform.position.y >= origPos.y + 0.05f)
            {
                curHeight -= 10 * Time.deltaTime;
                transform.position = new Vector3(origPos.x, curHeight, origPos.z);
            }
            else if (foot.TrigGround || curHeight <= origPos.y + 0.05f)
            {
                transform.position = origPos;
                floatState = 0;
                ChangeState(CharacterState.Idle);
                /*
                if (rotateDir==1)
                {
                    transform.localEulerAngles = Vector3.SmoothDamp(transform.localEulerAngles, new Vector3(0, 0, 0), ref oneClockwise, 0.1f);
                }
                else
                {
                    transform.localEulerAngles = Vector3.SmoothDamp(transform.localEulerAngles, new Vector3(0, 0, 0), ref oneCounterClockwise, 0.1f);
                }
                if (transform.localEulerAngles.z >= -0.02f && transform.localEulerAngles.z <= 0.02f)
                {
                    transform.localEulerAngles = new Vector3(0, 0, 0);
                    
                }*/
            }
        }
    }

    public override void BeHit(int damage, Vector3 force, bool changeHurt, float repelValue)
    {
        print("MoveCharacterBeHit");
        if (invincibleTime > 0)
        {
            return;
        }
        hp -= damage;
        uiHp.Show(hp, totalHp);
        if (hp <= 0)
        {
            rb.AddForce(force);
            ChangeState(CharacterState.Die);
            return;
        }
        else if (changeHurt && state == CharacterState.Weak)
        {
            print("ChangeHurt");
            rb.AddForce(force);
            ChangeState(CharacterState.Hurt);
            beRepelValue = totalBeRepelValue;
            return;
        }
        beRepelValue -= repelValue;
        if (changeHurt && beRepelValue <= 0)
        {
            print("ChangeWeak");
            ChangeState(CharacterState.Weak);
        }
    }
}
