/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ucp.model;

import java.util.ArrayList;
import ucp.plan.UcpAlgorithm;

/**
 * Class represents UML Use Case.
 * 
 * @author Trifolium Alpestre
 */
public class UcpUseCase {

private ArrayList<UcpUseCase> uList = new ArrayList<UcpUseCase>();    
private UcpMetrics metrics = new UcpMetrics();
private String name;
private String id;

/**
 * 
 * @return
 */
public String getName() {
        return name;
    }

/**
 * 
 * @return
 */
public UcpMetrics getMetrics() {
        return metrics;
    }

    /**
     * 
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 
     * @return
     */
    public String getId() {
        return id;
    }

    /**
     * 
     * @param id
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * 
     * @param ucpUseCase
     */
    public void addUseCase(UcpUseCase ucpUseCase) {
        this.getUList().add(ucpUseCase);
    }

    /**
     * 
     * @return
     */
    public ArrayList<UcpUseCase> getUList() {
        return uList;
    }
    
    
    
    /**
     * 
     * Method finds elements and sets coplexity.
     * 
     * @param idref
     * @param complexity
     */
    public void setComplexity(String idref, int complexity) {//prochazi model a hleda element s idref, kteremu nastavi complexity
        
        if (id.equals(idref)) //pokud ma usecase stejne id (idref)
        {
            metrics.setComplexity(complexity*5); //nastavi se mu complexity
            //Weight u Use Case je simple=5, average=10, complex=15 
            //ale v xmi exportu jsou hodnoty 1, 2, 3 proto nasobeni 5
        }
        
        else //jinak se hleda dal (hloubeji)
        
        {
      
        for (UcpUseCase u:uList)
        {
            u.setComplexity(idref, complexity); //preda hodnoty dal (hloubeji)
        }
        
        }
    }

    
    
    /**
     * Method sets metrics.
     * 
     * @param ucpMetrics
     */
    public void setMetrics(UcpMetrics ucpMetrics) { //jednoduche nastaveni metrics - pro usecase i subusecases nastavi stejne metrics
        
        this.metrics.setT(ucpMetrics.getT()); //technical factors
        this.metrics.setE(ucpMetrics.getE()); //environmental factors
        this.metrics.setHoursPerUc(ucpMetrics.getHoursPerUc()); //normovany cas prace na jeden UC
        
        for (UcpUseCase u:uList)
        {
            u.setMetrics(ucpMetrics); //preda hodnoty dal (hloubeji)        
        }
                
    }

    
    /**
     * Method counts UCP metrics according to defined level.
     * 
     * @param level
     */
    public void countUcp(int level) //vypocet ucp
    {
        UcpAlgorithm ucpAlgorithm = new UcpAlgorithm();
        int sumComplexity = 0;
        double sumHours = 0.0;
        double sumHoursSW = 0.0;
        
        for (UcpUseCase u:uList)
        {
            u.countUcp(level);//rekurzivni vypocet metrik u sub elementu
            
            //vyscitani zakladni metrics ze sub elementu do metrics vlastniho elementu
            if (u.getMetrics().getHours() > 0)//pokud ma subelement spocitane nenulove hodiny
            {//pak ma spocitane i cele metrics, takze se vyscitaji jen hodiny
                sumHours = sumHours + u.getMetrics().getHours();
                sumHoursSW = sumHoursSW + u.getMetrics().getHoursSW();
            }
            else
            {//jinak se vyscitaji jen complexity
                sumComplexity = sumComplexity + u.getMetrics().getComplexity();
            }
            
            //vyscitaji se i sumarni sub metriky ze sub elementu
            sumComplexity = sumComplexity + u.getMetrics().getSubUUCP();
            sumHours = sumHours + u.getMetrics().getSubCasesHours();
            sumHoursSW = sumHoursSW + u.getMetrics().getSubCasesHoursSW();
        }        
        
        //po pruchodu sub elementu se z pomocnych promennych nastavi sumarni sub metriky vlastniho elementu
        this.metrics.setSubUUCP(sumComplexity);
        this.metrics.setSubCasesHours(sumHours);
        this.metrics.setSubCasesHoursSW(sumHoursSW);
        
        //level = 3 -> pocita se kazdy element
        //level = 2 -> pocitaji se kompozitni usecase (tj. usecase s neprazdnym seznamem sub elementu)
        if ((level >= 3) || ((level >=2) && !(this.uList.isEmpty())))
        {
            //vypocet metrics pro vlastni element
            ucpAlgorithm.apply(this.metrics);
        }
    }
    
}
