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

public class ParameterContainer : MonoBehaviour
{
    public GameObject targetConObj;
    public GameObject blockConObj;
    public GameObject agentObj;
    private TargetController targetCon;
    private SceneBlockContainer blockCont;
    private float agentDistance;
    private int agentInArea;


    [Header("Env")]
    public bool lockMouse = false;
    public float Damage = 50; // damage to enemy
    public float fireRate = 0.5f;
    public int timeLimit = 30;
    public bool lockCameraX = false;
    public bool lockCameraY = true;
    public bool spawnAgentInAllMap = false;
    public int spinRecordMax = 20;
    public float spinPenaltyThreshold = 50;
    public float facingInviewEnemyDisCOEF = 0.5f;


    [Header("Dynamic Defaut Rewards")]
    //[Tooltip("Hit Enemy reward")]
    //public float hitRewardDefault = 60.0f;
    [Tooltip("Free mode Hit Enemy reward")]
    public float hitTargetRewardDefault = 60.0f;
    //[Tooltip("Enemy down reward")]
    //public float killRewardDefault = 60.0f;
    [Tooltip("Enemy down in area Reward")]
    public float killTargetEnemyRewardDefault = 100.0f;
    [Tooltip("stay in firebasesArea reward")]
    public float inAreaRewardDefault = 10.0f;
    [Tooltip("free left time bonus reward. ALLR + leftTime * r")]
    public float freeTimeBonusPerSec = 1.0f;
    [Tooltip("target left time bonus reward. ALLR + leftTime * r")]
    public float targetTimeBonusPerSec = 3.0f;
    [Tooltip("in area left time bonus reward. ALLR + leftTime * r")]
    public float areaTimeBonusPerSec = 1.0f;
    [Tooltip("distance reward reward = r*(1-(nowDis/startDis))")]
    public float distanceReward = 20.0f;
    [Space(10)]
    [Tooltip("Goto Win reward")]
    public float goWinRewardDefault = 100.0f;
    [Tooltip("Attack Win reward")]
    public float attackWinRewardDefault = 100.0f;
    [Tooltip("Defence Win reward")]
    public float defenceWinRewardDefault = 100.0f;
    [Tooltip("free Win reward")]
    public float freeWinRewardDefault = 100.0f;

    [Header("Static Rewards")]
    [Tooltip("Nothing happened reward")]
    public float nonReward = -0.05f;
    [Tooltip("Episode Lose reward")]
    public float loseReward = -0.05f;
    [Tooltip("Agent Do shoot action reward")]
    public float shootReward = -0.1f;
    [Tooltip("Hit Not target Enemy reward")]
    public float hitReward = 30.0f;
    [Tooltip("Not Target Enemy down reward")]
    public float killReward = 40.0f;
    [Tooltip("Agent Do shoot action but gun is not read")]
    public float shootWithoutReadyReward = -0.15f;
    [Tooltip("Kill bonus reward stack to nothing happend reward")]
    public float killBonusReward = 0.0f;
    [Tooltip("Facing to enemy's reward")]
    public float facingReward = 2.0f;
    [Tooltip("Shoot at target area but didn't hit enemy")]
    public float shootTargetAreaReward = 5.0f;

    [Header("Penalty Rewards")]
    [Tooltip("move Penalty Reward")]
    public float movePenalty = 0f;
    [Tooltip("spiiiiiiin Panalty Reward")]
    public float spinPenalty = 0f;
    [Tooltip("while move mouse a little bit's penalty")]
    public float mousePenalty = 0f;

    [Header("Dynamic Rewards")]
    [Tooltip("Free mode Hit Enemy reward")]
    public float hitTargetReward = 60.0f;
    [Tooltip("Enemy down in area Reward")]
    public float killTargetEnemyReward = 80.0f;
    [Tooltip("stay in firebasesArea reward")]
    public float inAreaReward = 1.0f;
    [Space(10)]
    [Tooltip("go Win reward")]
    public float goWinReward = 50.0f;
    [Tooltip("attack Win reward")]
    public float attackWinReward = 50.0f;
    [Tooltip("defence Win reward")]
    public float defenceWinReward = 50.0f;
    [Tooltip("free Win reward")]
    public float freeWinReward = 50.0f;

    private float targetTimeBonus = 0f;
    private float areaTimeBonus = 0f;
    private float freeTimeBonus = 0f;
    private float targetInAreaTime = 0f;
    private float lastFrameTime = 0f;

    private void Start()
    {
        targetCon = targetConObj.GetComponent<TargetController>();
        blockCont = blockConObj.GetComponent<SceneBlockContainer>();
        areaTimeBonus = areaTimeBonusPerSec * timeLimit;
        freeTimeBonus = freeTimeBonusPerSec * timeLimit;
        targetTimeBonus = targetTimeBonusPerSec * timeLimit;
    }

    private void Update()
    {
        if (targetCon.targetTypeInt != (int)TargetController.Targets.Free)
        {
            (agentDistance, agentInArea) = blockCont.getAgentTargetDistanceAndInside(agentObj.transform.position);
            // attack goto or defence target
            if (agentInArea == 1)
            {
                // agent out of area
                targetInAreaTime += Time.time - lastFrameTime;
            }
            areaTimeBonus = areaTimeBonusPerSec * (targetCon.leftTime + targetInAreaTime);
            freeTimeBonus = freeTimeBonusPerSec * targetCon.leftTime;
            targetTimeBonus = targetTimeBonusPerSec * targetCon.leftTime;
            lastFrameTime = Time.time;
        }
        else
        {
            // free target
            areaTimeBonus = areaTimeBonusPerSec * targetCon.leftTime;
            freeTimeBonus = freeTimeBonusPerSec * targetCon.leftTime;
            targetTimeBonus = targetTimeBonusPerSec * targetCon.leftTime;
        }

        hitTargetReward = hitTargetRewardDefault + targetTimeBonus;
        killTargetEnemyReward = killTargetEnemyRewardDefault + targetTimeBonus;
        inAreaReward = inAreaRewardDefault + areaTimeBonus;

        // Win Rewards
        goWinReward = goWinRewardDefault;
        attackWinReward = attackWinRewardDefault;
        defenceWinReward = defenceWinRewardDefault;
        freeWinReward = freeWinRewardDefault;
    }

    public void resetTimeBonusReward()
    {
        areaTimeBonus = areaTimeBonusPerSec * timeLimit;
        freeTimeBonus = freeTimeBonusPerSec * timeLimit;
        targetInAreaTime = 0f;
    }
}
