package com.bonus.common.core.utils;

import org.junit.Test;
import org.w3c.dom.Document;

import java.io.File;
import java.util.Map;
import java.util.Properties;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;


public class FileUtilsTest {

    File propertiesFile = new File("src/test/java/com/bonus/common/core/utils/test.properties");
    File ymlFile = new File("src/test/java/com/bonus/common/core/utils/test.yml");
    File xmlFile = new File("src/test/java/com/bonus/common/core/utils/test.xml");
    File jsonFile = new File("src/test/java/com/bonus/common/core/utils/test.json");
    File txtFile = new File("src/test/java/com/bonus/common/core/utils/test.txt");
    File svgFile = new File("src/test/java/com/bonus/common/core/utils/test.svg");
    File cimeFile = new File("src/test/java/com/bonus/common/core/utils/test.cime");

    @Test
    public void testReadProperties() throws Exception {
        Properties properties = ConfigFileUtils.readProperties(propertiesFile);
        assertEquals("value", properties.getProperty("key"));
        assertEquals("anotherValue", properties.getProperty("anotherKey"));
    }

    @Test
    public void testWriteProperties() throws Exception {
        Properties properties = ConfigFileUtils.readProperties(propertiesFile);
        properties.setProperty("anotherKey1", "anotherValue1");
        ConfigFileUtils.writeProperties(propertiesFile, properties);
        Properties loadedProperties = ConfigFileUtils.readProperties(propertiesFile);
        assertEquals("value", loadedProperties.getProperty("key"));
    }

    @Test
    public void testReadYml() throws Exception {
        Map<String, Object> ymlData = ConfigFileUtils.readYml(ymlFile);
        assertEquals("value", ymlData.get("key"));
        assertEquals("anotherValue", ymlData.get("anotherKey"));
    }

    @Test
    public void testWriteYml() throws Exception {
        Map<String, Object> ymlData = ConfigFileUtils.readYml(ymlFile);
        //Map<String, Object> ymlData = Collections.singletonMap("key", "value");
        ymlData.put("anotherKey1", "anotherValue1");
        ConfigFileUtils.writeYml(ymlFile, ymlData);
        Map<String, Object> loadedYmlData = ConfigFileUtils.readYml(ymlFile);
        assertEquals("value", loadedYmlData.get("key"));
    }

    @Test
    public void testReadXml() throws Exception {
        Document document = ConfigFileUtils.readXml(xmlFile);
        assertNotNull(document.getElementsByTagName("key").item(0));
        assertEquals("value", document.getElementsByTagName("key").item(0).getTextContent());
    }

    @Test
    public void testWriteXml() throws Exception {
        Document document = ConfigFileUtils.readXml(xmlFile);
        document.getDocumentElement().setAttribute("newAttribute", "newValue");

        ConfigFileUtils.writeXml(xmlFile, document);

        Document updatedDocument = ConfigFileUtils.readXml(xmlFile);
        assertEquals("newValue", updatedDocument.getDocumentElement().getAttribute("newAttribute"));
    }

    @Test
    public void testReadJson() throws Exception {
        Map<String, Object> jsonData = ConfigFileUtils.readJson(jsonFile, Map.class);
        assertEquals("value", jsonData.get("key"));
        assertEquals("anotherValue", jsonData.get("anotherKey"));
    }

    @Test
    public void testWriteJson() throws Exception {
        Map<String, Object> jsonData = ConfigFileUtils.readJson(jsonFile, Map.class);
        jsonData.put("anotherKey1", "anotherValue1");
        ConfigFileUtils.writeJson(jsonFile, jsonData);
        Map<String, Object> loadedJsonData = ConfigFileUtils.readJson(jsonFile, Map.class);
        assertEquals("value", loadedJsonData.get("key"));
    }

    @Test
    public void testReadTxt() throws Exception {
        String content = ConfigFileUtils.readTxt(txtFile);
        assertEquals("This is a test text file.", content);
    }

    @Test
    public void testWriteTxt() throws Exception {
        String newContent = "This is a test text file.";
        ConfigFileUtils.writeTxt(txtFile, newContent);

        String updatedContent = ConfigFileUtils.readTxt(txtFile);
        assertEquals(newContent, updatedContent);
    }

    @Test
    public void testReadSvg() throws Exception {
        String content = ConfigFileUtils.readSvg(svgFile);
        assertEquals("<svg>This is a test SVG file.</svg>", content);
    }

    @Test
    public void testWriteSvg() throws Exception {
        String newContent = "<svg>This is a test SVG file.</svg>";
        ConfigFileUtils.writeSvg(svgFile, newContent);

        String updatedContent = ConfigFileUtils.readSvg(svgFile);
        assertEquals(newContent, updatedContent);
    }

    @Test
    public void testReadCime() throws Exception {
        String content = ConfigFileUtils.readCime(cimeFile);
        assertEquals("This is a test CIME file.", content);
    }

    @Test
    public void testWriteCime() throws Exception {
        String newContent = "This is a test CIME file.";
        ConfigFileUtils.writeCime(cimeFile, newContent);

        String updatedContent = ConfigFileUtils.readCime(cimeFile);
        assertEquals(newContent, updatedContent);
    }

    @Test
    public void testReadPropertyByKey() throws Exception {
        String value = ConfigFileUtils.readPropertyByKey(propertiesFile, "key");
        assertEquals("value", value);
    }

    @Test
    public void testReadYmlByKey() throws Exception {
        Object value = ConfigFileUtils.readYmlByKey(ymlFile, "key");
        assertEquals("value", value);
    }

    @Test
    public void testReadJsonByKey() throws Exception {
        Object value = ConfigFileUtils.readJsonByKey(jsonFile, "key");
        assertEquals("value", value);
    }
}
