﻿using Assets.Scripts.Light;
using Assets.Scripts.Utility;
using UnityEngine;
using System.Collections.Generic;
using System.Collections;

namespace Assets.Scripts.Interactables
{
    public class PuzzleFilter : AbstractPuzzlePiece
    {
        public Vector3 FilterColor = Vector3.zero;
        private Color filterColor = Color.black;

        protected override void Start()
        {
            base.Start();

            filterColor = LightUtils.Vec3ToColor(FilterColor);

            SetupStructure();
        }
        private void SetupStructure()
        {
            foreach (Transform childTransform in transform)
            {
                GameObject child = childTransform.gameObject;
                switch (child.name.ToLower())
                {
                    case "collider":
                        child.tag = "filter";
                        break;
                    case "coloredfilter":
                        Renderer childRenderer = child.GetComponent<Renderer>();
                        if (childRenderer != null)
                        {
                            childRenderer.material.color = filterColor;
                        }
                        if (child.GetComponent<Collider>())
                        {
                            child.tag = "clickbox";
                        }
                        break;
                }
            }
        }

        public override void Hit(RaycastHit hitInfo, LaserSimulator laserInfo)
        {
            FirstTimePieceHit();
            ChangeMaterial = true;
            laserInfo.transform.position = new Vector3(transform.position.x, laserInfo.transform.position.y, transform.position.z);
            Color filteredColor = LightUtils.FilterColor(laserInfo.LaserColor, filterColor);
            if (LightUtils.LightVisible(filteredColor))
            {
                laserInfo.CreateNewLaser(laserInfo.transform.position, laserInfo.transform.forward, filteredColor);
            }

            laserInfo.EndSimulation();
        }

        public override void OnChange(){}
        
        public override string GetUnlocalizedName()
        {
            return "piece.filter";
        }
    }
}