var assert = require("assert");

var DocumentXmlReader = require("../lib/document-xml-reader").DocumentXmlReader;
var documents = require("../lib/documents");
var XmlElement = require("../lib/xmlreader").Element;

var testing = require("./testing");
var test = testing.test;
var createFakeDocxFile = testing.createFakeDocxFile;

function readXmlElement(element) {
    return new DocumentXmlReader({}).readXmlElement(element);
}

function readXmlElementValue(element) {
    var result = readXmlElement(element);
    assert.deepEqual(result.messages, []);
    return result.value;
}

describe("readXmlElement: ", function() {
    test("paragraph has no style if it has no properties", function() {
        var paragraphXml = new XmlElement("w:p", {}, []);
        var paragraph = readXmlElementValue(paragraphXml);
        assert.deepEqual(paragraph.styleName, undefined);
    });
    
    test("paragraph has style name read from paragraph properties if present", function() {
        var styleXml = new XmlElement("w:pStyle", {"w:val": "Heading1"}, []);
        var propertiesXml = new XmlElement("w:pPr", {}, [styleXml]);
        var paragraphXml = new XmlElement("w:p", {}, [propertiesXml]);
        var paragraph = readXmlElementValue(paragraphXml);
        assert.deepEqual(paragraph.styleName, "Heading1");
    });
    
    test("reads styles from run properties", function() {
        var runStyleXml = new XmlElement("w:rStyle", {"w:val": "Emphasis"});
        var runPropertiesXml = createRunPropertiesXml([runStyleXml]);
        var result = readXmlElement(runPropertiesXml);
        assert.equal(result.value.styleName, "Emphasis");
    });
    
    test("isBold is false if bold element is not present", function() {
        var runPropertiesXml = createRunPropertiesXml([]);
        var result = readXmlElement(runPropertiesXml);
        assert.equal(result.value.isBold, false);
    });
    
    test("isBold is true if bold element is present", function() {
        var boldXml = new XmlElement("w:b");
        var runPropertiesXml = createRunPropertiesXml([boldXml]);
        var result = readXmlElement(runPropertiesXml);
        assert.equal(result.value.isBold, true);
    });
    
    test("isItalic is false if bold element is not present", function() {
        var runPropertiesXml = createRunPropertiesXml([]);
        var result = readXmlElement(runPropertiesXml);
        assert.equal(result.value.isItalic, false);
    });
    
    test("isItalic is true if bold element is present", function() {
        var italicXml = new XmlElement("w:i");
        var runPropertiesXml = createRunPropertiesXml([italicXml]);
        var result = readXmlElement(runPropertiesXml);
        assert.equal(result.value.isItalic, true);
    });
    
    test("run properties are attached to run", function() {
        var runStyleXml = new XmlElement("w:rStyle", {"w:val": "Emphasis"});
        var runPropertiesXml = new XmlElement("w:rPr", {}, [runStyleXml]);
        var runXml = new XmlElement("w:r", {}, [runPropertiesXml]);
        var result = readXmlElement(runXml);
        assert.equal(result.value.styleName, "Emphasis");
    });
    
    test("run properties not included as child of run", function() {
        var runStyleXml = new XmlElement("w:rStyle", {"w:val": "Emphasis"});
        var runPropertiesXml = new XmlElement("w:rPr", {}, [runStyleXml]);
        var runXml = new XmlElement("w:r", {}, [runPropertiesXml]);
        var result = readXmlElement(runXml);
        assert.deepEqual(result.value.children, []);
    });
    
    test("emits warning on unrecognised element", function() {
        var unrecognisedElement = new XmlElement("w:not-an-element");
        var result = readXmlElement(unrecognisedElement);
        assert.deepEqual(
            result.messages,
            [{
                type: "warning",
                message: "An unrecognised element was ignored: w:not-an-element"
            }]
        );
    });
    
    test("w:bookmarkStart is ignored without warning", function() {
        var ignoredElement = new XmlElement("w:bookmarkStart");
        var result = readXmlElement(ignoredElement);
        assert.deepEqual(result.messages, []);
        assert.equal(null, result.value);
    });
    
    test("can read inline pictures", function() {
        var drawing = new XmlElement("w:drawing", {}, [
            new XmlElement("wp:inline", {}, [
                new XmlElement("wp:docPr", {descr: "It's a hat"}),
                new XmlElement("a:graphic", {}, [
                    new XmlElement("a:graphicData", {}, [
                        new XmlElement("pic:pic", {}, [
                            new XmlElement("pic:blipFill", {}, [
                                new XmlElement("a:blip", {"r:embed": "rId5"})
                            ])
                        ])
                    ])
                ])
            ])
        ]);
        
        var imageBuffer = new Buffer("Not an image at all!");
        var reader = new DocumentXmlReader(
            {
                "rId5": {target: "media/hat.png"}
            },
            createFakeDocxFile({
                "word/media/hat.png": imageBuffer
            })
        );
        var result = reader.readXmlElement(drawing);
        assert.deepEqual(result.messages, []);
        assert.equal("image", result.value.type);
        assert.equal(result.value.altText, "It's a hat");
        return result.value.read()
            .then(function(readValue) {
                assert.equal(readValue, imageBuffer)
            });
    });
});

function createRunPropertiesXml(children) {
    return new XmlElement("w:rPr", {}, children);
}

@@ -1333,19 +1333,248 @@
 t(%22r
-eads styles
+un has no style if it has no properties%22, function() %7B%0A        var runXml = runWithProperties(%5B%5D);%0A        var run = readXmlElementValue(runXml);%0A        assert.equal(run.styleName, undefined);%0A    %7D);%0A    %0A    test(%22run has style name read
  fro
@@ -1581,32 +1581,43 @@
 m run properties
+ if present
 %22, function() %7B%0A
@@ -1712,33 +1712,21 @@
  run
-PropertiesXml = createRun
+Xml = runWith
 Prop
@@ -1731,19 +1731,16 @@
 operties
-Xml
 (%5BrunSty
@@ -1753,37 +1753,34 @@
 );%0A        var r
-esult
+un
  = readXmlElemen
@@ -1776,38 +1776,33 @@
 adXmlElement
-(runProperties
+Value(run
 Xml);%0A      
@@ -1813,35 +1813,26 @@
 sert.equal(r
-esult.value
+un
 .styleName, 
@@ -5775,16 +5775,132 @@
 );%0A%7D);%0A%0A
+function runWithProperties(children) %7B%0A    return new XmlElement(%22w:r%22, %7B%7D, %5BcreateRunPropertiesXml(children)%5D);%0A%7D%0A%0A
 function

Add test to ensure style is undefined if run properties aren't present