/*
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;

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.groupon.jenkins.buildtype.install_packages.InstallPackagesBuild;
import com.groupon.jenkins.dynamic.build.repository.DynamicBuildRepository;
import com.groupon.jenkins.dynamic.build.repository.DynamicProjectRepository;
import com.groupon.jenkins.dynamic.buildtype.BuildType;
import com.groupon.jenkins.github.services.GithubAccessTokenRepository;
import hudson.Extension;
import jenkins.model.GlobalConfiguration;
import jenkins.model.Jenkins;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.StaplerRequest;

@Extension
public class SetupConfig extends GlobalConfiguration {
    private String dbHost;
    private int dbPort;
    private String dbName;
    private String githubApiUrl;
    private String githubCallbackUrl;
    private String label;
    private String fromEmailAddress;
    private String defaultBuildType;
<<<<<<< HEAD
    private boolean privateRepoSupport;
=======
    private String deployKey;
    private AbstractModule guiceModule;
    private transient Injector injector;
>>>>>>> origin/master

    public static SetupConfig get() {
        return GlobalConfiguration.all().get(SetupConfig.class);
    }

    public SetupConfig() {
        load();
    }

    @Override
    public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
        req.bindJSON(this, json);
        save();
        return true;
    }

    public Iterable<BuildType> getBuildTypes(){
        return BuildType.all();
    }
    public String getDbName() {
        if (StringUtils.isEmpty(dbName)) {
            return "dotci";
        }
        return dbName;
    }


    public void setDbName(String dbName) {
        this.dbName = dbName;
    }

    public String getDbHost() {
        if (StringUtils.isEmpty(dbHost)) {
            return "localhost";
        }
        return dbHost;
    }

    public void setDbHost(String dbHost) {
        this.dbHost = dbHost;
    }

    public int getDbPort() {
        if (dbPort == 0) {
            return 27017;
        }
        return dbPort;
    }

    public void setDbPort(int dbPort) {
        this.dbPort = dbPort;
    }

    public String getGithubApiUrl() {
        if (StringUtils.isEmpty(githubApiUrl)) {
            return "https://api.github.com/";
        }
        return githubApiUrl;
    }

    public void setGithubApiUrl(String githubApiUrl) {
        this.githubApiUrl = githubApiUrl;
    }

    public String getLabel() {
        return StringUtils.trimToEmpty(this.label);
    }

    public String getGithubCallbackUrl() {
        if (StringUtils.isEmpty(githubCallbackUrl)) {
            return Jenkins.getInstance().getRootUrl() + "/githook/";
        }
        return this.githubCallbackUrl;
    }

    public void setGithubCallbackUrl(String githubCallbackUrl) {
        this.githubCallbackUrl = githubCallbackUrl;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public String getFromEmailAddress() {
        if (StringUtils.isEmpty(fromEmailAddress)) {
            return "ci@example.com";
        }
        return fromEmailAddress;
    }

    public void setFromEmailAddress(String fromEmailAddress) {
        this.fromEmailAddress = fromEmailAddress;
    }


    public boolean hasPrivateRepoSupport() {
        return getPrivateRepoSupport();
    }


    public String getDefaultBuildType() {
        if (StringUtils.isEmpty(defaultBuildType)) {
            InstallPackagesBuild type = new InstallPackagesBuild();
            return type.getId();
        }
        return defaultBuildType;
    }

    public void setDefaultBuildType(String defaultBuildType) {
        this.defaultBuildType = defaultBuildType;
    }

<<<<<<< HEAD
    public void setPrivateRepoSupport(boolean privateRepoSupport) {
        this.privateRepoSupport = privateRepoSupport;
    }
    public boolean getPrivateRepoSupport() {
        return privateRepoSupport;
    }
=======
    public DynamicBuildRepository getDynamicBuildRepository() {
        return getInjector().getInstance(DynamicBuildRepository.class);
    }

    public DynamicProjectRepository getDynamicProjectRepository() {
        return getInjector().getInstance(DynamicProjectRepository.class);
    }

    public GithubAccessTokenRepository getGithubAccessTokenRepository() {
        return getInjector().getInstance(GithubAccessTokenRepository.class);
    }

    private transient Object injectorLock = new Object();

    public Injector getInjector() {
        if(injector == null) {
            synchronized (injectorLock) {
                if(injector == null) { // make sure we got the lock in time
                    injector = Guice.createInjector(getGuiceModule());
                }
            }
        }

        return injector;
    }


    private AbstractModule getGuiceModule() {
        if(guiceModule == null) {
            return new DotCiModule();
        }
        return guiceModule;
    }


>>>>>>> origin/master
}

@@ -2108,29 +2108,16 @@
 ldType;%0A
-%3C%3C%3C%3C%3C%3C%3C HEAD%0A
     priv
@@ -2148,24 +2148,16 @@
 upport;%0A
-=======%0A
     priv
@@ -2258,38 +2258,16 @@
 njector;
-%0A%3E%3E%3E%3E%3E%3E%3E origin/master
 %0A%0A    pu
@@ -4898,21 +4898,8 @@
  %7D%0A%0A
-%3C%3C%3C%3C%3C%3C%3C HEAD%0A
     
@@ -5112,16 +5112,8 @@
   %7D%0A
-=======%0A
     
@@ -6115,30 +6115,6 @@
   %7D%0A
-%0A%0A%3E%3E%3E%3E%3E%3E%3E origin/master%0A
 %7D%0A

Fix merge conflicts