package Model;

import java.util.concurrent.ConcurrentLinkedQueue;

public class MyModel extends Thread {

    private Object oThreadLock;

    private ConcurrentLinkedQueue<ModelData> qDataToMain;

    private int iModelCommand = 2;

    private ModelData oModelData;

    public MyModel(Object oThreadLock, ConcurrentLinkedQueue<ModelData> qDataToMain) {
        this.oThreadLock = oThreadLock;
        this.qDataToMain = qDataToMain;
    }

    @Override
    public void run() {
        this.setName("Model");
        System.out.println("Model running");
        while (iModelCommand != 0) {
            //Model will be suspended at start until main notifies, that new task has to be executed.
            synchronized(oThreadLock) {
                try {
                    System.out.println("Model suspended");
                    oThreadLock.wait();
                } catch (InterruptedException e) {
                    System.out.println("Error at oThreadLock.wait() at MyModel");
                    e.printStackTrace();
                }
            }
            System.out.println("Model unsuspended");
            System.out.println("Command: " + iModelCommand);
            switch (iModelCommand) {
                case (1): {

                }break;
                case (2): {

                }break;
                case (3): {

                }break;
                case (4): {

                }break;
                case (5): {

                }break;
                case (6): {

                }break;
                case (7): {

                }break;
                case (8): {

                }break;
                case (9): {

                }break;
                case (10): {

                }break;
                case (11): {

                }break;
                case (34): {
                    ModelCommands.exercise34(this);
                }break;
                case (35): {

                }break;
            }
        }

        System.out.println("Model-Thread stopped.");
    }

    public void setModelCommand(int iModelCommand) {
        this.iModelCommand = iModelCommand;
    }

    public void setModelData(ModelData oModelData) {
        this.oModelData = oModelData;
    }

    public ModelData getModelData() {
        return this.oModelData;
    }

    public ConcurrentLinkedQueue<ModelData> getModelQueueToMain() {
        return this.qDataToMain;
    }
}