package leigh.minecraft.link;

import leigh.eo.EO;
import leigh.mecha.log.MechaLogger;
import leigh.mecha.log.MechaLoggerFactory;
import leigh.minecraft.link.quickshop.QuickShopReader;
import leigh.minecraft.link.test.MockPlayerNameProvider;
import org.junit.jupiter.api.Test;

import java.io.File;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

public class QuickShopReaderTest {
    private static final MechaLogger logger = MechaLoggerFactory.getLogger(QuickShopReaderTest.class);
    
    @Test
    public void testThing() throws Exception {
        String currentDir = System.getProperty("user.dir");
        logger.info("CWD: {}", currentDir);

        //QuickShopReader reader = new QuickShopReader(new File("/Users/aleigh/massive/export.txt"));
        QuickShopReader reader = new QuickShopReader(new File("quickshops.txt"),
                new MockPlayerNameProvider("junit"));

        // Size check - Test file has 448 entries but some known malformed ones.
        // 442 is the expected number of good values from 'export.txt'.
        logger.info("Asserting number of shops");
        assertEquals(442, reader.getShops().size());

        // Check that enchantment names have been converted to friendly text
        // by checking the second character of the enchant string is lowercase.
        logger.info("Asserting all enchantment names converted to friendly text.");
        for (EO shop : reader.getShops()) {
            if (shop.contains("enchants")) {
                logger.debug("ITEM TYPE: {}", shop.getValueString("item"));
                for (EO enchant : shop.getValueLoop("enchants")) {
                    logger.debug("ENCHANT: {}", enchant.getValueString("name"));
                    if (Character.isUpperCase(enchant.getValueString("name").charAt(1))) {
                        fail("Enchant name not converted to friendly text");
                    }
                }
            }
        }

        // Check that item names have a friendly name and it's Title Case.
        logger.info("Asserting all items have a friendly name.");
        for (EO shop : reader.getShops()) {
            logger.debug("ITEM TYPE: {}", shop.getValueString("item"));
            if (shop.contains("itemDisplayName")) {
                logger.debug("ITEM FRIENDLY NAME: {}", shop.getValueString("item_friendly"));
                String friendlyItemName = shop.getValueString("itemDisplayName");
                if (!Character.isUpperCase(friendlyItemName.charAt(0)) || !Character.isLowerCase(friendlyItemName.charAt(1)) ) {
                    fail("Friendly item name not title case for [" + friendlyItemName + "]");
                }
            } else {
               fail("No friendly name found for [" + shop.getValueString("item") + "]");
            }
        }
    }
}
