﻿using UnityEngine;
using System.Collections;

public class Sct_TrapControl : MonoBehaviour
{
    /// <summary>
    /// 放陷阱的蟲
    /// </summary>
    public Sct_LavaBodyControl OwnerLava
    {
        set
        {
            _ownerLava = value;
        }
        get
        {
            return _ownerLava;
        }
    }
    private Sct_LavaBodyControl _ownerLava;

    /// <summary>
    /// 夾到後要處理的事
    /// </summary>
    /// <param name="_catchedLava">被夾到的蟲</param>
    public void Catch(Sct_LavaBodyControl _catchedLava)
    {
        //是自己放的就沒事
        if (_catchedLava == _ownerLava)
            return;

        if (_catchedLava && _ownerLava)
        {
            Sct_LavaState _targetState = _catchedLava.GetComponent<Sct_LavaState>();
            //Sct_BubbleMask _targetMask = _catchedLava.GetComponent<Sct_BubbleMask>();
            
            if (_targetState)
            {
                //當撞到的是蝴蝶..
                if (_targetState.CurrentState == LavaState.ButterFly)
                {
                    _targetState.Check2Lava(2);
                }
                else
                {
                    //刪除被夾到蟲的身體
                    if (_catchedLava.LavaBodys.Count == 3)
                    {
                        _catchedLava.RemoveBody();
                        _catchedLava.RemoveBody();
                    }
                    else if (_catchedLava.LavaBodys.Count == 2)
                        _catchedLava.RemoveBody();
                }
            }
            else
            {
                Debug.LogError("檢查是否有放Component!");
            }


            Sct_LavaState _myState = _ownerLava.GetComponent<Sct_LavaState>();

            if (_myState)
            {
                if (_myState.CurrentState == LavaState.Lava)
                {
                    if (_ownerLava.LavaBodys.Count == 3)
                    {
                        //增加放陷阱的蟲身體
                        _ownerLava.AddBody();
                        _ownerLava.AddBody();
                    }
                    else if (_ownerLava.LavaBodys.Count == 2)
                        _ownerLava.RemoveBody();
                }

                _myState.Check2Butterfly();
            }
        }
        else
        {
            Debug.LogError("檢查是否有放Component!");
        }

        //做完事後刪除自己
        Destroy(gameObject);
    }

	void Start ()
    {
        
	}
	

	void Update ()
    {
	    
	}
}
