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

public class Boss : MonoBehaviour {

    public GameObject boss;
    private EnemyHealth enemyHealth;

    public Transform dropLocation;
    public float dropDelay;
    private float dropTimer;

    public Transform leftSpawn;
    public Transform rightSpawn;

    private BOSSSTATE bossState;

    public float stateDuration;
    public float stateTimer;

    public bool isBossLeft;
    public bool isBossRight;

    public bool isBossDead;

    public GameObject endOfGameLockBox;

    public bool isBossActive;

    private enum BOSSSTATE {
        RIGHTSPAWNACTIVE,
        LEFTSPAWNACTIVE
    }

    void Awake() {
        enemyHealth = boss.GetComponent<EnemyHealth>();
    }

    void Start() {

        isBossActive = false;
        isBossDead = false;

        isBossRight = true;

        bossState = BOSSSTATE.RIGHTSPAWNACTIVE;
    }

    void FixedUpdate() {

        BossDeathCheck();
        StateChanger();

        if(isBossActive) {
            if(stateTimer > 0) {
                stateTimer -= Time.deltaTime;

            } else if(!isBossDead && isBossRight) {
                bossState = BOSSSTATE.RIGHTSPAWNACTIVE;

                ResetBossState();

            } else if(!isBossDead && isBossLeft) {
                bossState = BOSSSTATE.LEFTSPAWNACTIVE;

                ResetBossState();

            } else if(isBossDead) {
                //boss.SetActive(false);

            } else {
                // left blank intentionally
            }
        }
    }

    private void BossDeathCheck() {

        if(enemyHealth.currentHealth <= 0) {
            isBossDead = true;
            boss.SetActive(false);

            endOfGameLockBox.SetActive(true);

        } else {
            isBossDead = false;
        }
    }

    private void StateChanger() {

        switch(bossState) {
            case BOSSSTATE.RIGHTSPAWNACTIVE:

                isBossRight = false;
                isBossLeft = true;

                boss.transform.position = new Vector2(rightSpawn.position.x, boss.transform.position.y);

                break;

            case BOSSSTATE.LEFTSPAWNACTIVE:

                isBossRight = true;
                isBossLeft = false;

                boss.transform.position = new Vector2(leftSpawn.position.x, boss.transform.position.y);

                break;

            default:
                return;
        }
    }

    private void ResetBossState() {
        stateTimer = stateDuration;
    }

    private void SpawnBossDrops() {

        if(dropTimer > 0) {
            dropTimer -= Time.deltaTime;

        } else {
            GameObject _enemy = EnemyPool.s_enemyPool.RetrieveEnemy();

            dropLocation.position = new Vector2(Random.Range(leftSpawn.position.x , rightSpawn.position.x) , dropLocation.position.y);

            _enemy.transform.position = dropLocation.position;
            _enemy.SetActive(true);

            dropTimer = dropDelay;
        }
    }

    private void OnTriggerStay2D(Collider2D other) {
        
        if(!isBossDead && other.tag == "Player") {

            isBossActive = true;

            SpawnBossDrops();
        }
    }
}
