/*                          -----------------------
 *                          SEXY CRAZY WORMSY CUBES
 *                          -----------------------
 *                    Copyright(c)2015-2016 Jonas Sjöberg
 *                       https://github.com/jonasjberg
 *                            jomeganas@gmail.com
 *______________________________________________________________________________
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *                    (at your option) any later version.
 
 *      This program is distributed in the hope that it will be useful,
 *       but WITHOUT ANY WARRANTY; without even the implied warranty of
 *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *                GNU General Public License for more details.
 
 *     You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *______________________________________________________________________________
 */

package com.jonasjberg.wormsycubes.main;


import com.jonasjberg.wormsycubes.Main;


public class SlowMotion
{
    private static final int FORCED_ACTIVE_TIMEOUT = 100;

    private SlowMotionState state;
    private int             slowMotionTime;
    private boolean         tryToActivate;
    private boolean         disengage;
    private boolean         isActive;
    private boolean         forcedActive;
    private int             forcedActiveTimer;

    public SlowMotion()
    {
        init();
    }

    public void init()
    {
        Main.logger.fine("initialized slow motion ..");
        slowMotionTime = GameConstants.SLOWMOTION_TIME_MAX;
        state = SlowMotionState.FULL;
        tryToActivate = disengage = isActive = forcedActive = false;
        forcedActiveTimer = FORCED_ACTIVE_TIMEOUT;
    }

    public void updateSlowmoStateMachine()
    {
        if (forcedActive) {
            if (forcedActiveTimer > 0) {
                forcedActiveTimer--;
                // System.out.println("forcedActiveTimer--");
            } else {
                forcedActiveTimer = FORCED_ACTIVE_TIMEOUT;
                forcedActive = isActive = false;
                return;
            }

            isActive = true;
            return;
        }

        switch (state) {
            case DEFAULT: {
                Main.logger.finest("Entered state DEFAULT");
                if (tryToActivate
                    && slowMotionTime > GameConstants.SLOWMOTION_TIME_MIN) {
                    state = SlowMotionState.ACTIVATED;
                } else if (slowMotionTime < GameConstants.SLOWMOTION_TIME_MAX) {
                    state = SlowMotionState.CHARGING;
                }
            }
            break;

            case ACTIVATED: {
                Main.logger.finest("Entered state ACTIVATED");
                tryToActivate = false;

                if (disengage) {
                    // System.out.println("DISENGAGE");
                    state = SlowMotionState.DEFAULT;
                    isActive = false;
                    break;
                }

                if (slowMotionTime <= GameConstants.SLOWMOTION_TIME_MIN) {
                    slowMotionTime = GameConstants.SLOWMOTION_TIME_MIN;
                    state = SlowMotionState.EMPTY;
                    isActive = false;
                    break;
                } else if (slowMotionTime > GameConstants.SLOWMOTION_TIME_MIN) {
                    slowMotionTime--;
                    isActive = true;
                    break;
                }
            }
            break;

            case AVAILABLE: {
                // Main.logger.finest("Entered state AVAILABLE");
                // if (tryToActivate) {
                //     tryToActivate = false;
                //     state = SlowMotionState.ACTIVATED;
                //     break;
                // } else if (!tryToActivate) {
                //     isActive = false;
                // }
            }
            break;

            case EMPTY: {
                Main.logger.finest("Entered state EMPTY");
                isActive = false;
                state = SlowMotionState.CHARGING;
            }
            break;

            case FULL: {
                Main.logger.finest("Entered state FULL");
                state = SlowMotionState.DEFAULT;
            }
            break;

            case CHARGING: {
                Main.logger.finest("Entered state CHARGING");
                if (slowMotionTime > GameConstants.SLOWMOTION_TIME_MIN
                    && tryToActivate) {
                    state = SlowMotionState.ACTIVATED;
                    break;
                } else if (slowMotionTime < GameConstants.SLOWMOTION_TIME_MAX) {
                    slowMotionTime = slowMotionTime
                                     + (1 *
                                        GameConstants.SLOWMOTION_RECHARGE_RATE);
                } else if (slowMotionTime >=
                           GameConstants.SLOWMOTION_TIME_MAX) {
                    slowMotionTime = GameConstants.SLOWMOTION_TIME_MAX;
                    state = SlowMotionState.FULL;
                }
            }
            break;

            default:
                break;
        }
    }

    public boolean slowMotionIsCharging()
    {
        return state == SlowMotionState.CHARGING;
    }

    public boolean slowMotionIsAvailable()
    {
        return state == SlowMotionState.AVAILABLE;
    }

    public boolean slowMotionIsActive()
    {
        return isActive;
    }

    public void tryActivate()
    {
        tryToActivate = true;
        disengage = false;
    }

    public void forceActive()
    {
        forcedActive = true;
    }

    public void disengage()
    {
        disengage = true;
    }

    public int getSlowMotionTime()
    {
        return slowMotionTime;
    }
}
