﻿using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Security.Policy;
using DG.Tweening;
using UnityEngine;
using DG.Tweening;
using DarkTonic.MasterAudio;

public class Snake : MonoBehaviour
{

    public GameState gameState;

    public string pieceTitle = "Piece #";

    public GameObject[] snakeParts;
    public GameObject anchorHelper;
    public JointController jointController;

    public SnakeControls controls;

    public int snakeLength;

    public float pieceMassMax = 2f;
    public float pieceMassMin = 0.1f;

    public PieceSpawner pieceSpawner;


    public bool switchedControls = false;
    public float switchTimer;
    public float switchDuration = 10.0f;

    public GameObject particlesConfused;

    // Use this for initialization
    void Start ()
    {
        particlesConfused.SetActive(false);
    }
	
	// Update is called once per frame
	void Update () {

	    if (switchedControls)
	    {

            if (switchTimer >= switchDuration)
	        {
	            switchTimer = 0;
	            switchedControls = false;
	            particlesConfused.SetActive(false);
            }
	        else
            {
                particlesConfused.SetActive(true);
                switchTimer += Time.deltaTime;
            }
        }

	}

    public void StartGame()
    {
        snakeLength = 0;
        // new array
        snakeParts = new GameObject[1000];
    }

    public void AddItem(GameObject item)
    {
        pieceSpawner.currentPieces--;

        for (int i = 0; i < snakeParts.Length; i++)
        {
            if (snakeParts[i] != null) snakeParts[i].GetComponent<SnakePiece>().Bulge(0.05f * i);
            if (snakeParts[i] == null)
            {
                SnakePiece snakePiece = item.GetComponent<SnakePiece>();

                snakeParts[i] = item;
                // Debug.Log("Added item to array position " + i);
                item.name = pieceTitle + i;
                snakePiece.connected = true;

                if (i == 0) // Is it first in the array?
                {
                    Debug.Log("First Item");
                    item.transform.position = anchorHelper.transform.position;
                    snakePiece.Connect(this.gameObject, i, this); // Connect it to the crab
                }
                else
                {
                    item.transform.position = snakeParts[i - 1].GetComponent<SnakePiece>().anchorHelper.transform.position;
                    snakePiece.Connect(snakeParts[i - 1], i, this); // Connect it to the previous item
                }
                break;
            }
        }

        UpdateMass();
        UpdateMassPieces();
        jointController.UpdateStringStrength();
    }

    public float massStart = 50;
    public float pieceMassFactor = 2;

    public void UpdateMass()
    {
        this.gameObject.GetComponent<Rigidbody>().mass = massStart + (pieceMassFactor * snakeLength);
    }

    public void UpdateMassPieces()
    {
        float step = (pieceMassMax - pieceMassMin)/snakeLength;
        for (int i = 0; i < snakeLength; i++)
        {
            // Debug.Log("Mass for piece #" +i + ": " + (pieceMassMax - (step * i)));
            snakeParts[i].GetComponent<Rigidbody>().mass = pieceMassMax - (step * i);
        }
    }

    public Material matDeathPiece;

    public GameObject snakeHeadModel;

    void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.CompareTag("SnakePiece"))
        {
            // Add Item if not on tower
            if (!collision.gameObject.GetComponent<SnakePiece>().connected)
            {
                AddItem(collision.gameObject);
            }
            else // Check if player loses
            {
                if (collision.gameObject.GetComponent<SnakePiece>().position >= 4)
                {
                    if (gameState.state == GameState.GameStates.Over) return;
                    collision.gameObject.GetComponent<Renderer>().material = matDeathPiece;
                    snakeHeadModel.GetComponent<Renderer>().material.color = Color.red;
                    Debug.Log("GAME LOST!");
                    gameState.GameOver();
                }
            }
        }
    }
}
