/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014, Groupon, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.groupon.jenkins.dynamic.build;

import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import hudson.Extension;
import hudson.model.Job;
import hudson.model.JobProperty;
import hudson.model.JobPropertyDescriptor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.StaplerRequest;

import javax.annotation.Nullable;

public class DynamicProjectBranchTabsProperty extends JobProperty<Job<?, ?>> {
    private final ArrayList<String> branches;
    private String branchTabs;
    public DynamicProjectBranchTabsProperty(String branchTabs) {
        this.branchTabs = branchTabs;
        this.branches = parseBranches();
    }

    private ArrayList<String> parseBranches() {
        ArrayList<String> parsedBranches = new ArrayList<String>();
        if(!StringUtils.isEmpty(branchTabs)){
            parsedBranches.addAll(trim());
        }
        return parsedBranches;
    }

    private List<String> trim() {
        return Lists.newArrayList( Iterables.transform(Arrays.asList( branchTabs.split("\\n")), new Function<String, String>() {
            @Override
            public String apply(@Nullable String input) {
                return input.trim();
            }
        }));
    }


    public List<String> getBranches(){
        ArrayList<String> branchList = new ArrayList<String>();
        if(!branchList.contains("Mine")) branchList.add("Mine");
        if(!branchList.contains("All")) branchList.add("All");
        branchList.addAll(branches);
        return branchList;
    }

    public String getBranchTabs() {
        return branchTabs;
    }

    public void addBranch(String branch) {
        this.branches.add(branch);
        save();
    }


    public void removeBranch(String branch) {
        this.branches.remove(branch);
        save();
    }

    private void save() {
        this.branchTabs= Joiner.on(",").join(branches);
    }

    @Extension
    public static final class BuildTypePropertyDescriptor extends JobPropertyDescriptor {

        @Override
        public String getDisplayName() {
            return "Build Type";
        }

        @Override
        public boolean isApplicable(Class<? extends Job> jobType) {
            return DynamicProject.class.equals(jobType);
        }

        @Override
        public DynamicProjectBranchTabsProperty newInstance(StaplerRequest req, JSONObject formData) throws FormException {
            return new DynamicProjectBranchTabsProperty(formData.getString("branchTabs"));
        }
    }
}

@@ -1794,16 +1794,26 @@
 private 
+transient 
 final Ar
@@ -2627,16 +2627,80 @@
 ches()%7B%0A
+        ArrayList%3CString%3E configuredBranches = parseBranches();%0A
         
@@ -2763,34 +2763,42 @@
         if(!
-b
+configuredB
 ranch
-List
+es
 .contains(%22M
@@ -2840,26 +2840,34 @@
     if(!
-b
+configuredB
 ranch
-List
+es
 .contain
@@ -2925,17 +2925,27 @@
 .addAll(
-b
+configuredB
 ranches)
@@ -3321,17 +3321,18 @@
 ner.on(%22
-,
+%5Cn
 %22).join(
@@ -3399,16 +3399,17 @@
 ss B
-uildType
+ranchTabs
 Prop
@@ -3540,17 +3540,18 @@
 n %22B
-uild Type
+ranch Tabs
 %22;%0A 

Save tabs on newlines not commas