/**
 * Copyright (c) 2009-2015, rultor.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met: 1) Redistributions of source code must retain the above
 * copyright notice, this list of conditions and the following
 * disclaimer. 2) Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following
 * disclaimer in the documentation and/or other materials provided
 * with the distribution. 3) Neither the name of the rultor.com nor
 * the names of its contributors may be used to endorse or promote
 * products derived from this software without specific prior written
 * permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.rultor.agents.github.qtn;

import com.google.common.collect.ImmutableMap;
import com.jcabi.aspects.Immutable;
import com.jcabi.github.Comment;
import com.jcabi.github.Issue;
import com.jcabi.github.Pull;
import com.jcabi.log.Logger;
import com.rultor.agents.github.Answer;
import com.rultor.agents.github.Question;
import com.rultor.agents.github.Req;
import java.io.IOException;
import java.net.URI;
import java.util.ResourceBundle;
import javax.json.JsonObject;
import lombok.EqualsAndHashCode;
import lombok.ToString;

/**
 * Merge request.
 *
 * @author Yegor Bugayenko (yegor@tpc2.com)
 * @version $Id$
 * @since 1.3
 * @checkstyle MultipleStringLiteralsCheck (500 lines)
 */
@Immutable
@ToString
@EqualsAndHashCode
public final class QnMerge implements Question {

    /**
     * Message bundle.
     */
    private static final ResourceBundle PHRASES =
        ResourceBundle.getBundle("phrases");

    @Override
    public Req understand(final Comment.Smart comment,
        final URI home) throws IOException {
        final Issue.Smart issue = new Issue.Smart(comment.issue());
        final Req req;
        if (issue.isPull() && issue.isOpen()) {
            new Answer(comment).post(
                true,
                String.format(
                    QnMerge.PHRASES.getString("QnMerge.start"),
                    home.toASCIIString()
                )
            );
            Logger.info(
                this, "merge request found in %s#%d, comment #%d",
                issue.repo().coordinates(), issue.number(), comment.number()
            );
            req = QnMerge.pack(
                comment,
                issue.repo().pulls().get(issue.number())
            );
        } else if (issue.isOpen()) {
            new Answer(comment).post(
                false,
                QnMerge.PHRASES.getString("QnMerge.not-pull-request")
            );
            req = Req.EMPTY;
        } else {
            new Answer(comment).post(
                false,
                QnMerge.PHRASES.getString("QnMerge.already-closed")
            );
            req = Req.EMPTY;
        }
        return req;
    }

    /**
     * Pack a pull request.
     * @param comment The comment we're in
     * @param pull Pull
     * @return Req
     * @throws IOException If fails
     */
    private static Req pack(final Comment.Smart comment,
        final Pull pull) throws IOException {
        final JsonObject head = pull.json().getJsonObject("head");
        final JsonObject base = pull.json().getJsonObject("base");
        final Req req;
        final String repo = "repo";
        if (head.isNull(repo)) {
            new Answer(comment).post(
                false,
                QnMerge.PHRASES.getString("QnMerge.head-is-gone")
            );
            req = Req.EMPTY;
        } else if (base.isNull(repo)) {
            new Answer(comment).post(
                false,
                QnMerge.PHRASES.getString("QnMerge.base-is-gone")
            );
            req = Req.EMPTY;
        } else {
            req = new Req.Simple(
                "merge",
                new ImmutableMap.Builder<String, String>()
                    .put("pull_id", Integer.toString(pull.number()))
                    .put("pull_title", new Issue.Smart(comment.issue()).title())
                    .put("fork_branch", head.getString("ref"))
                    .put("head_branch", base.getString("ref"))
                    .put(
                        "head",
                        String.format(
                            "git@github.com:%s.git",
                            base.getJsonObject(repo).getString("full_name")
                        )
                    )
                    .put(
                        "fork",
                        String.format(
                            "git@github.com:%s.git",
                            head.getJsonObject(repo).getString("full_name")
                        )
                    )
                    .build()
            );
        }
        return req;
    }

}

@@ -1773,16 +1773,49 @@
 b.Pull;%0A
+import com.jcabi.github.PullRef;%0A
 import c
@@ -2040,38 +2040,8 @@
 le;%0A
-import javax.json.JsonObject;%0A
 impo
@@ -3995,26 +3995,23 @@
   final 
-JsonObject
+PullRef
  head = 
@@ -4019,35 +4019,13 @@
 ull.
-json().getJsonObject(%22
 head
-%22
+(
 );%0A 
@@ -4037,26 +4037,23 @@
   final 
-JsonObject
+PullRef
  base = 
@@ -4061,35 +4061,13 @@
 ull.
-json().getJsonObject(%22
 base
-%22
+(
 );%0A 
@@ -4141,16 +4141,23 @@
 f (head.
+json().
 isNull(r
@@ -4359,16 +4359,23 @@
 f (base.
+json().
 isNull(r
@@ -4883,31 +4883,20 @@
 %22, head.
-getString(%22
 ref
-%22
+(
 ))%0A     
@@ -4939,23 +4939,12 @@
 ase.
-getString(%22
 ref
-%22
+(
 ))%0A 
@@ -5129,49 +5129,38 @@
 ase.
-getJsonObject(repo).getString(%22full_name%22
+repo().coordinates().toString(
 )%0A  
@@ -5392,49 +5392,38 @@
 ead.
-getJsonObject(repo).getString(%22full_name%22
+repo().coordinates().toString(
 )%0A  

use PullRef API for getting head+base