package com.wizzardo.http.framework.template.taglib;

import com.wizzardo.http.framework.Controller;
import com.wizzardo.http.framework.WebApplicationTest;
import com.wizzardo.http.framework.template.*;
import com.wizzardo.http.framework.template.taglib.g.Link;
import com.wizzardo.tools.xml.Node;
import org.junit.Assert;
import org.junit.Test;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by wizzardo on 26.04.15.
 */
public class LinkTest extends WebApplicationTest {

    public static class BookController extends Controller {

        public Renderer list() {
            return renderString("list of books");
        }

        public Renderer get() {
            return renderString("some book");
        }
    }

    @Override
    public void setUp() throws NoSuchMethodException, ClassNotFoundException, NoSuchFieldException {
        super.setUp();
        TagLib.findTags(Collections.singletonList(Link.class));
        server.getUrlMapping()
                .append("/book/list", BookController.class, "list")
                .append("/book/$id", BookController.class, "get")
        ;
    }

    @Test
    public void testLinkTag() {
        Map<String, String> attrs = new LinkedHashMap<String, String>();
        Map<String, Object> model = new LinkedHashMap<String, Object>();

        attrs.put("controller", "book");
        attrs.put("action", "list");
        Assert.assertEquals("<a href=\"/book/list\"/>\n", new Link(new LinkedHashMap<>(attrs), null).get(model).toString());

        attrs.put("absolute", "true");
        Assert.assertEquals("<a href=\"http://localhost:9999/book/list\"/>\n", new Link(new LinkedHashMap<>(attrs), null).get(model).toString());

        attrs.put("base", "http://ya.ru");
        Assert.assertEquals("<a href=\"http://ya.ru/book/list\"/>\n", new Link(new LinkedHashMap<>(attrs), null).get(model).toString());

        attrs.put("fragment", "some_fragment");
        Assert.assertEquals("<a href=\"http://ya.ru/book/list#some_fragment\"/>\n", new Link(new LinkedHashMap<>(attrs), null).get(model).toString());

        attrs.put("params", "[key:'value']");
        Assert.assertEquals("<a href=\"http://ya.ru/book/list?key=value#some_fragment\"/>\n", new Link(new LinkedHashMap<>(attrs), null).get(model).toString());

        attrs.put("params", "[key:'value', key2: 'value2']");
        Assert.assertEquals("<a href=\"http://ya.ru/book/list?key=value&key2=value2#some_fragment\"/>\n", new Link(new LinkedHashMap<>(attrs), null).get(model).toString());


        attrs.clear();
        attrs.put("controller", "book");
        attrs.put("action", "get");
        attrs.put("params", "[id:1]");
        Assert.assertEquals("<a href=\"/book/1\"/>\n", new Link(new LinkedHashMap<>(attrs), null).get(model).toString());

        attrs.put("class", "red");
        attrs.put("id", "link1");
        Assert.assertEquals("<a href=\"/book/1\" class=\"red\" id=\"link1\"/>\n", new Link(new LinkedHashMap<>(attrs), null).get(model).toString());


        attrs.clear();
        attrs.put("controller", "book");
        attrs.put("action", "get");
        attrs.put("params", "[id:id]");
        model.put("id", 1);
        Assert.assertEquals("<a href=\"/book/1\"/>\n", new Link(new LinkedHashMap<>(attrs), null).get(model).toString());

        attrs.put("id", "link_${id}");
        Assert.assertEquals("<a href=\"/book/1\" id=\"link_1\"/>\n", new Link(new LinkedHashMap<>(attrs), null).get(model).toString());
    }

    @Test
    public void testInTemplate() {
        Node n = Node.parse("<div><g:link controller=\"book\" action=\"get\" params=\"[id: 1]\">text</g:link></div>", true);

        RenderableList l = new RenderableList();
        ViewRenderer.prepare(n.children(), l, "", "");

        Assert.assertEquals("" +
                "<div>\n" +
                "    <a href=\"/book/1\">\n" +
                "        text\n" +
                "    </a>\n" +
                "</div>\n", l.get(new Model()).toString());
    }
}

@@ -1481,32 +1481,39 @@
 /%3E%5Cn%22, new Link(
+).init(
 new LinkedHashMa
@@ -1514,38 +1514,32 @@
 HashMap%3C%3E(attrs)
-, null
 ).get(model).toS
@@ -1668,32 +1668,39 @@
 /%3E%5Cn%22, new Link(
+).init(
 new LinkedHashMa
@@ -1701,38 +1701,32 @@
 HashMap%3C%3E(attrs)
-, null
 ).get(model).toS
@@ -1850,32 +1850,39 @@
 /%3E%5Cn%22, new Link(
+).init(
 new LinkedHashMa
@@ -1883,38 +1883,32 @@
 HashMap%3C%3E(attrs)
-, null
 ).get(model).toS
@@ -2051,32 +2051,39 @@
 /%3E%5Cn%22, new Link(
+).init(
 new LinkedHashMa
@@ -2084,38 +2084,32 @@
 HashMap%3C%3E(attrs)
-, null
 ).get(model).toS
@@ -2260,32 +2260,39 @@
 /%3E%5Cn%22, new Link(
+).init(
 new LinkedHashMa
@@ -2293,38 +2293,32 @@
 HashMap%3C%3E(attrs)
-, null
 ).get(model).toS
@@ -2497,32 +2497,39 @@
 /%3E%5Cn%22, new Link(
+).init(
 new LinkedHashMa
@@ -2530,38 +2530,32 @@
 HashMap%3C%3E(attrs)
-, null
 ).get(model).toS
@@ -2761,32 +2761,39 @@
 /%3E%5Cn%22, new Link(
+).init(
 new LinkedHashMa
@@ -2794,38 +2794,32 @@
 HashMap%3C%3E(attrs)
-, null
 ).get(model).toS
@@ -2981,32 +2981,39 @@
 /%3E%5Cn%22, new Link(
+).init(
 new LinkedHashMa
@@ -3014,38 +3014,32 @@
 HashMap%3C%3E(attrs)
-, null
 ).get(model).toS
@@ -3274,32 +3274,39 @@
 /%3E%5Cn%22, new Link(
+).init(
 new LinkedHashMa
@@ -3307,38 +3307,32 @@
 HashMap%3C%3E(attrs)
-, null
 ).get(model).toS
@@ -3459,16 +3459,23 @@
 ew Link(
+).init(
 new Link
@@ -3496,14 +3496,8 @@
 trs)
-, null
 ).ge

fix tests