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

public class EnvChanger : MonoBehaviour
{
    private float initAmbientIntensity;
    private float initRefIntensity;
    [SerializeField]
    private float targetAmbientIntensity, targetRefIntensity;
    [SerializeField]
    private GameObject targetLightOutside, targetLightInside;
    [SerializeField]
    private bool enterFromLeft = true;
    [SerializeField]
    private Transform playerPos;
    private bool isInEnviro = false;
    public void ChangeEnvironnement(){
        
        if(ExitOnCorrectSide()){
            isInEnviro = !isInEnviro;
            if(isInEnviro){
                initAmbientIntensity = RenderSettings.ambientIntensity;
                initRefIntensity = RenderSettings.reflectionIntensity;
                RenderSettings.ambientIntensity = targetAmbientIntensity;
                RenderSettings.reflectionIntensity = targetRefIntensity;
                targetLightInside.SetActive(true);
                targetLightOutside.SetActive(false);
            }
            else{
                RenderSettings.ambientIntensity = initAmbientIntensity;
                RenderSettings.reflectionIntensity = initRefIntensity;
                targetLightInside.SetActive(false);
                targetLightOutside.SetActive(true);
            }
        }
        
    }

    private bool ExitOnCorrectSide(){
        
        if(enterFromLeft){
            if(!isInEnviro){
                if(playerPos.position.x > transform.position.x){
                    
                    return true;
                }
            }else{
                if(playerPos.position.x < transform.position.x){
                    
                    return true;
                }
            }
            
        }else{
            if(!isInEnviro){
                if(playerPos.position.x < transform.position.x){
                    return true;
                }
            }else{
                if(playerPos.position.x > transform.position.x){
                    return true;
                }
            }
        }
        
        return false;
    }
}
