/*
 * 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 Model.
 * 
 * @author Trifolium Alpestre
 */
public class UcpModel {
    
private ArrayList<UcpPackage> pList = new ArrayList<UcpPackage>();
private UcpMetrics metrics = new UcpMetrics();
private String name;

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

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

    /**
     * 
     * @param ucpPackage
     */
    public void addPackage(UcpPackage ucpPackage) {
        this.getPList().add(ucpPackage);
    }

    /**
     * 
     * @return
     */
    public ArrayList<UcpPackage> getPList() {
        return pList;
    }

    
    /**
     * 
     * 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
        
        for (UcpPackage p:pList)
        {
           p.setComplexity(idref, complexity); //preda hodnoty dal (hloubeji)        
        }
        
    }

    /**
     * @return the metrics
     */
    public UcpMetrics getMetrics() {
        return metrics;
    }
    
    /**
     * Method sets metrics.
     * 
     * @param ucpMetrics
     */
    public void setMetrics(UcpMetrics ucpMetrics) { //jednoduche nastaveni metrics - vsem elementum v modelu nastavi stejne metrics
        
        this.getMetrics().setT(ucpMetrics.getT()); //technical factors
        this.getMetrics().setE(ucpMetrics.getE()); //environmental factors
        this.getMetrics().setHoursPerUc(ucpMetrics.getHoursPerUc()); //normovany cas prace na jeden UC
        
        for (UcpPackage p:pList)
        {
            p.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 (UcpPackage p:pList)
        {
            p.countUcp(level);//rekurzivni vypocet metrik u sub elementu
            
            //vyscitani zakladni metrics ze sub elementu do metrics vlastniho elementu
            if (p.getMetrics().getHours() > 0)//pokud ma subelement spocitane nenulove hodiny
            {//pak ma spocitane i cele metrics, takze se vyscitaji jen hodiny
                sumHours = sumHours + p.getMetrics().getHours();
                sumHoursSW = sumHoursSW + p.getMetrics().getHoursSW();
            }
            else
            {//jinak se vyscitaji jen complexity
                sumComplexity = sumComplexity + p.getMetrics().getComplexity();
            }
            
            //vyscitaji se i sumarni sub metriky ze sub elementu
            sumComplexity = sumComplexity + p.getMetrics().getSubUUCP();
            sumHours = sumHours + p.getMetrics().getSubCasesHours();
            sumHoursSW = sumHoursSW + p.getMetrics().getSubCasesHoursSW();
        }
               
        //po pruchodu sub elementu se z pomocnych promennych nastavi sumarni sub metriky vlastniho elementu
        this.getMetrics().setSubUUCP(sumComplexity);
        this.getMetrics().setSubCasesHours(sumHours);
        this.getMetrics().setSubCasesHoursSW(sumHoursSW);
        
        //level = 0 -> pocita se root (tj. jen model jako celek)
        if (level >= 0)
        {
            //vypocet metrics pro vlastni element
            ucpAlgorithm.apply(this.getMetrics());
        }
    }

    
}
