

package com.ltsllc.miranda.manager.states;


import com.ltsllc.miranda.Message;
import com.ltsllc.miranda.manager.Manager;
import com.ltsllc.miranda.shutdown.ShutdownResponseMessage;
import com.ltsllc.miranda.State;
import com.ltsllc.miranda.StopState;
import com.ltsllc.miranda.clientinterface.MirandaException;

import java.util.concurrent.BlockingQueue;

/**
 * Tell the associated file to shut down and wait for a reply.
 *
 * <p>
 *     Once the class gets a reply it sends a reply of is own and goes into the {@link StopState}.
 * </p>
 */
public class ManagerShuttingDownState extends ManagerState {
    private BlockingQueue<Message> requester;

    public BlockingQueue<Message> getRequester() {
        return requester;
    }

    public ManagerShuttingDownState(Manager manager, BlockingQueue<Message> requester) throws MirandaException {
        super(manager);

        this.requester = requester;
    }

    public State start () {
        getManager().getFile().sendShutdown(getManager().getQueue(), this);
        return this;
    }

    public State processMessage(Message message) throws MirandaException {
        State nextState = getManager().getCurrentState();

        switch (message.getSubject()) {
            case ShutdownResponse: {
                ShutdownResponseMessage shutdownResponseMessage = (ShutdownResponseMessage) message;
                nextState = processShutdownResponseMessage(shutdownResponseMessage);
                break;
            }

            default: {
                nextState = super.processMessage(message);
                break;
            }
        }

        return nextState;
    }


    public State processShutdownResponseMessage(ShutdownResponseMessage shutdownResponseMessage) {
        ShutdownResponseMessage shutdownResponseMessage2 = new ShutdownResponseMessage(getManager().getQueue(), this,
                getManager().getName());

        send(getRequester(), shutdownResponseMessage2);

        return StopState.getInstance();
    }
}
