package io.jenkins.blueocean.service.embedded.rest;

import hudson.model.Job;
import hudson.model.Queue;
import io.jenkins.blueocean.commons.ServiceException;
import io.jenkins.blueocean.rest.hal.Link;
import io.jenkins.blueocean.rest.model.BluePipeline;
import io.jenkins.blueocean.rest.model.BlueQueueContainer;
import io.jenkins.blueocean.rest.model.BlueQueueItem;
import jenkins.model.Jenkins;
import org.jenkinsci.plugins.workflow.support.steps.ExecutorStepExecution;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author Vivek Pandey
 */
public class MultiBranchPipelineQueueContainer extends BlueQueueContainer {

    private final MultiBranchPipelineImpl multiBranchPipeline;
    private final Link self;

    public MultiBranchPipelineQueueContainer(MultiBranchPipelineImpl multiBranchPipeline) {
        this.multiBranchPipeline = multiBranchPipeline;
        this.self = multiBranchPipeline.getLink().rel("queue");
    }

    @Override
    public BlueQueueItem get(String name) {
        try {
            Queue.Item item = Jenkins.getActiveInstance().getQueue().getItem(Long.parseLong(name));
            if(item != null){
                PipelineImpl pipeline = (PipelineImpl) multiBranchPipeline.getBranches().get(item.task.getOwnerTask().getName());
                if(pipeline != null) {

                    if(item.task instanceof ExecutorStepExecution.PlaceholderTask) {
                        ExecutorStepExecution.PlaceholderTask task = (ExecutorStepExecution.PlaceholderTask) item.task;
                        int runNumber;
                        if(task.run() == null){
                            runNumber = pipeline.job.getNextBuildNumber();
                        }else{
                            runNumber = task.run().getNumber();
                        }
                        return new QueueItemImpl(item, item.task.getOwnerTask().getName(), runNumber,
                            self.rel(String.valueOf(item.getId())));
                    }

                }
            }
        }catch (NumberFormatException e){
            throw new ServiceException.BadRequestExpception("Invalid queue id: "+name+". Must be a number.",e);
        }
        return null;
    }

    @Override
    public Link getLink() {
        return self;
    }

    @Override
    public Iterator<BlueQueueItem> iterator() {
        final List<BlueQueueItem> items = new ArrayList<>();
        Map<String,List<Queue.Item>> queueMap = new HashMap<>();

        for(Queue.Item item: Jenkins.getActiveInstance().getQueue().getItems()){
            if(item.task instanceof ExecutorStepExecution.PlaceholderTask){
                ExecutorStepExecution.PlaceholderTask task = (ExecutorStepExecution.PlaceholderTask) item.task;
                String ownerTaskName = task.getOwnerTask().getName();
                List<Queue.Item> its = queueMap.get(task.getOwnerTask().getName());
                if(its == null){
                    its = new ArrayList<>();
                    queueMap.put(ownerTaskName,its);
                }
                its.add(item);
            }
        }
        for(final BluePipeline p:multiBranchPipeline.getBranches()){
            Job job =  ((PipelineImpl)p).job;
            List<Queue.Item> its = queueMap.get(job.getName());
            if(its == null || its.isEmpty()){
                continue;
            }
            int count=0;
            for(Queue.Item item:its){
                ExecutorStepExecution.PlaceholderTask task = (ExecutorStepExecution.PlaceholderTask) item.task;
                if(task != null){
                    int runNumber;
                    if(task.run() == null){
                        runNumber = job.getNextBuildNumber() + count;
                        count++;
                    }else{
                        runNumber = task.run().getNumber();
                    }
                    items.add(new QueueItemImpl(item,p.getName(),
                        runNumber, self.rel(String.valueOf(item.getId()))));
                }
            }
        }
        return items.iterator();
    }
}

@@ -1596,47 +1596,8 @@
 sk;%0A
-                        int runNumber;%0A
     
@@ -1673,51 +1673,65 @@
    r
-unNumber = pipeline.job.getNextBuildNumber(
+eturn QueueContainerImpl.getQueuedItem(item, pipeline.job
 );%0A 
@@ -1768,94 +1768,8 @@
     
-                        runNumber = task.run().getNumber();%0A                        %7D%0A
     
@@ -1855,25 +1855,38 @@
 Name(), 
-run
+task.run().get
 Number
+()
 ,%0A      
@@ -1907,16 +1907,20 @@
         
+ 
+   
 self.rel
@@ -1948,24 +1948,50 @@
 getId())));%0A
+                        %7D%0A
             

Use utitlity method for generating queue item