/*
 * Copyright 2012 s_wolff.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.aegee.runanddine.status;

import java.util.HashMap;
import java.util.Map;

import org.aegee.runanddine.util.data.ModelNotExistingException;
import org.aegee.runanddine.util.model.Model;

/**
 * Model for persistent RunAndDineStatus
 * @author s_wolff
 */
public class RunAndDineStatus extends Model {
    private static final long serialVersionUID = 1L;

    /**
     * ID of RunAndDine the status is related to
     */
    int runAndDineId;
    
    /**
     * Map containing all status related to a RunAndDine
     */
    Map<String, String> stati;

    public RunAndDineStatus() {
        this.stati = new HashMap<String, String>();
    }

    public RunAndDineStatus(int runAndDineId) {
        this.runAndDineId = runAndDineId;
        this.stati = new HashMap<String, String>();
    }

    public RunAndDineStatus(int runAndDineId, Map<String, String> stati) {
        this.runAndDineId = runAndDineId;
        this.stati = stati;
    }

    /**
     * Set ID of RunAndDine the status is related to
     * @param runAndDineId ID of related RunAndDine
     */
    public void setRunAndDineId(int runAndDineId) {
        this.runAndDineId = runAndDineId;
    }
    
    /**
     * Get ID of RunAndDine the status is related to
     * @return ID of related RunAndDine
     */
    public int getRunAndDineId() {
        return this.runAndDineId;
    }

    /**
     * Add new status information
     * @param key Key of status
     * @param value Value of status
     */
    public void putStatusInformation(String key, String value) {
        this.stati.put(key, value);
        RunAndDineStatusManager.getInstance().save(this);
    }

    /**
     * Get status information by key
     * @param key Key of the status to be received
     * @return Value of the status identified by the given key
     */
    public String getStatusInformation(String key) {
        return this.stati.get(key);
    }

    /**
     * Get each status information
     * @return Map containing each available status information
     */
    public Map<String, String> getStatusInformation() {
        return new HashMap<String, String>(this.stati);
    }

    /**
     * Set status information for a step of a RunAndDine
     * @param runAndDineId ID of related RunAndDine
     * @param clz Class identifying a step of the RunAndDine
     * @param value Value to be set for the status
     */
    public static void setStatusInformation(int runAndDineId, Class<?> clz, String value) {
        RunAndDineStatus stat;
        try {
            stat = RunAndDineStatusManager.getInstance().getById(runAndDineId);
        } catch (ModelNotExistingException e) {
            stat = new RunAndDineStatus(runAndDineId);
            RunAndDineStatusManager.getInstance().save(stat);
        }
        stat.putStatusInformation(clz.getName(), value);
    }
}
