/*************************GO-LICENSE-START*********************************
 * Copyright 2014 ThoughtWorks, Inc.
 *
 * 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.
 *************************GO-LICENSE-END***********************************/

package com.thoughtworks.go.server.presentation.models;

import java.util.Collection;
import java.util.Map;

import com.thoughtworks.go.config.PipelineConfig;
import com.thoughtworks.go.domain.StageIdentifier;
import com.thoughtworks.go.presentation.pipelinehistory.PipelineInstanceModel;
import com.thoughtworks.go.presentation.pipelinehistory.PipelineInstanceModels;
import com.thoughtworks.go.presentation.pipelinehistory.StageInstanceModels;

public class PipelineInstanceGroupModel {
    private StageConfigurationModels config;
    private PipelineInstanceModels pipelineInstances;
    private Map<String, StageIdentifier> latestStages;

    public void setLatestStages(Map<String, StageIdentifier> latestStages) {
        this.latestStages = latestStages;
    }

    public Map<String, StageIdentifier> getLatestStages() {
        return latestStages;
    }

    public PipelineInstanceGroupModel() {
        this(new StageConfigurationModels());
    }

    public PipelineInstanceGroupModel(StageConfigurationModels config) {
        this.config = config;
        pipelineInstances = PipelineInstanceModels.createPipelineInstanceModels();
    }

    public Collection<StageConfigurationModel> getStages() {
        return config;
    }

    public PipelineInstanceModels getPipelineInstances() {
        return pipelineInstances;
    }

    public boolean hasSameStagesAs(PipelineInstanceModel pipelineInstanceModel) {
        StageInstanceModels stageHistory = pipelineInstanceModel.getStageHistory();
        if (config.size() != stageHistory.size()) {
            return false;
        }
        for (int i = 0; i < config.size(); i++) {
            if (!equals(config.get(i), stageHistory.get(i))) {
                return false;
            }
        }
        return true;
    }

    public boolean match(PipelineConfig pipelineConfig) {
        return config.match(pipelineConfig);
    }

    private boolean equals(StageConfigurationModel obj1, StageConfigurationModel obj2) {
        return obj1.getName().equals(obj2.getName()) && obj1.isAutoApproved() == obj2.isAutoApproved();
    }

    public void add(PipelineInstanceModel instance) {
        pipelineInstances.add(instance);
    }

    public PipelineInstanceModel findPipelineInstance(String pipelineLabel) {
        if("latest".equals(pipelineLabel) && !pipelineInstances.isEmpty()){
            return pipelineInstances.first();
        }
        
        for (PipelineInstanceModel instance : pipelineInstances) {
            if (instance.getLabel().equals(pipelineLabel)) {
                return instance;
            }
        }
        return null;
    }
}

@@ -1267,22 +1267,22 @@
 nModels 
-config
+stages
 ;%0A    pr
@@ -1765,22 +1765,22 @@
 nModels 
-config
+stages
 ) %7B%0A    
@@ -1792,23 +1792,23 @@
 his.
-config = config
+stages = stages
 ;%0A  
@@ -1971,22 +1971,22 @@
  return 
-config
+stages
 ;%0A    %7D%0A
@@ -2264,22 +2264,22 @@
     if (
-config
+stages
 .size() 
@@ -2368,22 +2368,22 @@
  0; i %3C 
-config
+stages
 .size();
@@ -2414,22 +2414,22 @@
 !equals(
-config
+stages
 .get(i),
@@ -2608,22 +2608,22 @@
  return 
-config
+stages
 .match(p

Rename PipelineInstanceGroupModel's config -> stages