/**
 * Copyright (c) 2009-2014, 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.twitter;

import com.jcabi.aspects.Immutable;
import com.jcabi.aspects.Tv;
import com.jcabi.github.Github;
import com.jcabi.github.Issue;
import com.jcabi.github.Repo;
import com.jcabi.log.Logger;
import com.jcabi.xml.XML;
import com.rultor.agents.AbstractAgent;
import com.rultor.agents.github.TalkIssues;
import java.io.IOException;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;
import org.xembly.Directive;
import org.xembly.Directives;

/**
 * Tweets.
 *
 * @author Yegor Bugayenko (yegor@tpc2.com)
 * @version $Id$
 * @since 1.30
 */
@Immutable
@ToString
@EqualsAndHashCode(callSuper = false, of = { "github", "twitter" })
public final class Tweets extends AbstractAgent {

    /**
     * Github.
     */
    private final transient Github github;

    /**
     * Twitter.
     */
    private final transient Twitter twitter;

    /**
     * Ctor.
     * @param ghub Github client
     * @param twt Twitter client
     */
    public Tweets(final Github ghub, final Twitter twt) {
        super(
            "/talk/wire[github-repo and github-issue]",
            "/talk/request[@id and type='release' and success='true']"
        );
        this.github = ghub;
        this.twitter = twt;
    }

    @Override
    public Iterable<Directive> process(final XML xml) throws IOException {
        final XML req = xml.nodes("/talk/request").get(0);
        final Issue.Smart issue = new TalkIssues(this.github, xml).get();
        final Repo.Smart repo = new Repo.Smart(issue.repo());
        if (!repo.isPrivate()) {
            this.twitter.post(
                String.format(
                    "%s, %s released https://github.com/%s",
                    StringUtils.substring(
                        repo.description(),
                        0, Tv.HUNDRED
                    ),
                    req.xpath("args/arg[@name='tag']/text()").get(0),
                    issue.repo().coordinates()
                )
            );
            Logger.info(
                this, "tweet posted about %s release",
                issue.repo().coordinates()
            );
        }
        return new Directives();
    }

}

@@ -3208,20 +3208,19 @@
     
-String.forma
+Tweets.twee
 t(%0A 
@@ -3242,48 +3242,62 @@
     
-%22%25s, %25s released https://github.com/%25s%22,
+repo, req.xpath(%22args/arg%5B@name='tag'%5D/text()%22).get(0)
 %0A   
@@ -3312,35 +3312,148 @@
     
+ )%0A
      
-StringUtils.substring(
+       );%0A            Logger.info(%0A                this, %22tweet posted about %25s release%22,%0A                issue.repo().coordinates()
 %0A   
@@ -3465,203 +3465,468 @@
     
+);%0A
        
+ %7D%0A
      
-repo.description(),
+   return new Directives();
 %0A    
+%7D%0A%0A
     
+/**%0A
      
-           0, Tv.HUNDRED%0A                    ),%0A      
+* Create a tweet to post.%0A     * @param repo The repo%0A     * @param tag The tag%0A     * @return Tweet text%0A     * @throws IOException If fails%0A
      
+*/%0A
     
-     req.xpath(%22args/arg%5B@name='tag'%5D/text()%22).get(0),%0A                    issue.
+private static String tweet(final Repo.Smart repo, final String tag)%0A        throws IOException %7B%0A        final StringBuilder text = new StringBuilder(2 * Tv.HUNDRED);%0A        if (repo.description().isEmpty()) %7B%0A            text.append(
 repo
-()
 .coo
@@ -3935,34 +3935,42 @@
 inates()
-%0A                )
+.repo());%0A        %7D else %7B
 %0A       
@@ -3966,34 +3966,44 @@
 e %7B%0A            
-);
+text.append(
 %0A            Log
@@ -4003,173 +4003,311 @@
     
-Logger.info(%0A                this, %22tweet posted about %25s release%22,%0A                issue.repo().coordinates()%0A            );%0A        %7D%0A        return new Directives
+    StringUtils.substring(%0A                    repo.description(),%0A                    0, Tv.HUNDRED%0A                )%0A            );%0A        %7D%0A        return text.append(%22, %22).append(tag)%0A            .append(%22 released https://github.com/%22)%0A            .append(repo.coordinates())%0A            .toString
 ();%0A

#643 tweet text improved