<?php
ini_set("display_errors", "on");
error_reporting(E_ALL ^ E_NOTICE);
require('commonFunctions.php');

$type = strtolower($_GET['type']);

$elementIn = array("?Element: Fire?", "?Element: Ice?", "?Element: Wind?", "?Element: Earth?", "?Element: Thunder?", "?Element: Water?", "?Element: Light?", "?Element: Dark?", "?Element: Air?");
$elementShort = array("Fire", "Ice", "Air", "Earth", "Thunder", "Water", "Light", "Dark", "Wind");
$elementOut = array("©", "¢", "£", "¤", "¥", "§", "®", "²", "£");
$cutList = array('Square of', 'Jug of', 'Vial of', 'Piece of', 'Jar of', 'Pot of', 'Vase of', 'Sprig of', 'Bag of', 'Clump of', 'Set of', 'Ingot of', 'Handful of', 'Spool of', 'Ponze of', 'Bowl of', 'Bundle of', 'Flask of', 'Flasque of', 'Lump of', 'Chunk of', 'Slice of', 'Serving of', 'Bunch of', 'Plate of', 'Bottle of', 'Sprig of', 'Dish of', 'Can of', 'Pair of', 'Loaf of', 'Cone of', 'Pitcher of', 'Pair of', 'Dose of');
$cutListPl = array('Squares of', 'Jugs of', 'Vials of', 'Pieces of', 'Jars of', 'Pots of', 'Vases of', 'Sprigs of', 'Bags of', 'Clumps of', 'Sets of', 'Ingots of', 'Handfuls of', 'Spools of', 'Ponzes of', 'Bowls of', 'Bundles of', 'Flasks of', 'Flasques of', 'Lumps of', 'Chunks of', 'Slices of', 'Servings of', 'Bunches of', 'Plates of', 'Bottles of', 'Sprigs of', 'Dishes of', 'Cans of', 'Pairs of', 'Loaves of', 'Cones of', 'Pitchers of', 'Pairs of', 'Doses of');
$repList = array('Square', 'Jug', 'Vial', 'Piece', 'Jar', 'Pot', 'Vase', 'Sprig', 'Bag', 'Clump', 'Set', 'Ingot', 'Handful', 'Spool', 'Ponze', 'Bowl', 'Bundle', 'Flask', 'Flasque', 'Lump', 'Chunk', 'Slice', 'Serving', 'Bunch', 'Plate', 'Bottle', 'Sprig', 'Dish', 'Can', 'Pair', 'Loaf', 'Cone', 'Pitcher', 'Pair', 'Dose');
$repListPl = array('Squares', 'Jugs', 'Vials', 'Pieces', 'Jars', 'Pots', 'Vases', 'Sprigs', 'Bags', 'Clumps', 'Sets', 'Ingots', 'Handfuls', 'Spools', 'Ponzes', 'Bowls', 'Bundles', 'Flasks', 'Flasques', 'Lumps', 'Chunks', 'Slices', 'Servings', 'Bunches', 'Plates', 'Bottles', 'Sprigs', 'Dishes', 'Cans', 'Pairs', 'Loaves', 'Cones', 'Pitchers', 'Pairs', 'Doses');
$skillList = array('Woodworking', 'Goldsmithing', 'Bonecraft', 'Cooking', 'Smithing', 'Fishing', 'Alchemy', 'Leathercraft', 'Clothcraft', '-');
$skillRanks = array('Amateur', 'Recruit', 'Initiate', 'Novice', 'Apprentice', 'Journeyman', 'Craftsman', 'Artisan', 'Adept', 'Veteran');
$specialWeaponIn = array("Ochain", "Daurdabla", "Verethragna", "Twashtar", "Almace", "Caladbolg", "Farsha", "Ukonvasara", "Redemption", "Rhongomiant", "Kannagi", "Masamune", "Gambanteinn", "Hvergelmir", "Gandiva", "Armageddon", "Aegis", "Gjallarhorn", "Spharai", "Mandau", "Excalibur", "Ragnarok", "Guttler", "Bravura", "Gungnir", "Apocalypse", "Kikoku", "Amanomurakumo", "Mjollnir", "Claustrum", "Annihilator", "Yoichinoyumi", "Conqueror", "Glanzfaust", "Yagrush", "Laevateinn", "Murgleis", "Vajra", "Burtgang", "Liberator", "Aymur", "Carnwenhan", "Gastraphetes", "Kogarasumaru", "Nagi", "Ryunohige", "Nirvana", "Tizona", "Death Penalty", "Kenkonken", "Terpsichore", "Tupsimati");
$specialWeaponOut = array("Empyrean", "Empyrean", "Empyrean", "Empyrean", "Empyrean", "Empyrean", "Empyrean", "Empyrean", "Empyrean", "Empyrean", "Empyrean", "Empyrean", "Empyrean", "Empyrean", "Empyrean", "Empyrean", "Relic", "Relic", "Relic", "Relic", "Relic", "Relic", "Relic", "Relic", "Relic", "Relic", "Relic", "Relic", "Relic", "Relic", "Relic", "Relic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic", "Mythic");

if (strlen($type) > 0) {
    $fileList = array('item1Data.txt', 'item2Data.txt', 'weaponsData.txt', 'armorData.txt', 'vouchersData.txt');
    $fileName = array('item1', 'item2', 'weapons', 'armor', 'vouchers');
	$nameList = array();
	$names = array();
	$data = array();
    $idList = array();
    foreach ($fileList as $k => $fl) {
        $array = @file($fl);
        $fs = $fileName[$k];
        foreach ($array as $num => $row) {
            $newRow = explode("|", $row);
            $id = getIDVal($newRow[1]);
            if ($type == 'data' || $type == 'redirect') {
                $data[$id] = $row;
            } elseif ($type == 'goodlist') {
                array_push($idList, $id);
            } else {
                $longName = ucitem($newRow[4]);
                $wikiName = $newRow[2];
                $link = ($longName != $wikiName) ? "$wikiName|$longName" : $wikiName;
                $addendum = (strpos($newRow[7], "?") > 0)? "&nbsp; <a href=\"Image-O-Matic.php?fileSystem=items1&id=$num&replace=on\">Enable Smart replacement</a>" : '';
                $nameList[$id] = ($type == 'image') ? "* [[<a href=\"Image-O-Matic.php?fileSystem=$fs&id=$num\">$link</a>]]$addendum" : "* [[<a href=\"Item-O-Matic.php?&ind=$id\">$link</a>]]";
                $names[$id] = strtolower($newRow[4]);
            }
        }
    }
}
if (strlen($type) == 0) {
    $list = array('data', 'image', 'list', 'goodlist', 'order', 'redirect');
	print "<b>List-O-Matic!</b>";
	foreach ($list as $l) {
	    print "\r\n<br /><a href=\"List-O-Matic.php?&type=$l\">$l</a>";
	}
} elseif ($type == 'redirect') {
    $searchName = array();
    foreach($data as $key => $currow) {
        $thisdata = explode('|', $currow);
        $thisdata[4] = ucitem($thisdata[4]);
        $thisdata[5] = ucitem($thisdata[5]);
        $doPhrase = false;
        $augPhrase = '';
        $augPhrases = '';
        $cutPhrase = str_replace($cutList, "", $thisdata[4]);
        $cutPhrase = trim($cutPhrase);
        $cutPhrases = str_replace($cutListPl, "", $thisdata[5]);
        $cutPhrases = trim($cutPhrases);
        $checkPhrase = ($cutPhrase != $thisdata[4]) ? "True" : "False";
        if ($cutPhrase != $thisdata[4] || $cutPhrase != $thisdata[3]) {
            foreach ($cutList as $i => $t) {
                if (strpos($thisdata[4], $t) !== false) {
                    if (strpos($thisdata[4], $t) !== false && strpos($thisdata[3], $repList[$i]) === false) {
                        $augPhrase = $cutPhrase." ".$repList[$i];
                        $doPhrase = true;
                    }
                    if ($cutPhrase." ".$repList[$i] != $thisdata[3] && $augPhrase == '') {
                        $augPhrase = $cutPhrase." ".$repList[$i];
                        $doPhrase = true;
                    }
                    if (strpos($thisdata[5], $cutListPl[$i]) !== false && strpos($thisdata[3], $repListPl[$i]) === false) {
                        $augPhrases = $cutPhrases." ".$repListPl[$i];
                        $doPhrase = true;
                    }
                }
            }
        }
        //print "<b>".$thisdata[3]." / ".$thisdata[4]."</b>: $cutPhrase  |  $cutPhrases  || $checkPhrase - $doPhrase ||  $augPhrase | $augPhrases\r\n<br />";
        if ($searchName[$thisdata[3]] == null) {
            $sname = ($thisdata[3] == $thisdata[2]) ? '' : $thisdata[3];
            $searchName[$thisdata[3]] = $sname;
            $special = str_replace($specialWeaponIn, 'Special', $sname);
            if ($special != 'Special') {
                if ($thisdata[3] != $thisdata[4]) {
                    $searchName[$thisdata[4]] = $thisdata[2];
                }
                if ($thisdata[3] != $cutPhrase) {
                    $searchName[$cutPhrase] = $thisdata[2];
                }
                if ($doPhrase == true) {
                    $searchName[$augPhrase] = $thisdata[2];
                    $searchName[$augPhrases] = $thisdata[2];
                }
                if ($thisdata[3] != $thisdata[5]) {
                    $searchName[$thisdata[5]] = $thisdata[2];
                }
                if ($thisdata[3] != $cutPhrases) {
                    $searchName[$cutPhrases] = $thisdata[2];
                }
            }
        }

    }
    $searchName['Lightweight Steel'] = "Lt. Steel Sheet";
    foreach ($searchName as $key => $row) {
        if (strlen($row) > 0) {
            print "$key|$row<br />\r\n";
        } else {
            print "$key<br />\r\n";
        }
    }
} elseif ($type == 'data') {
    $arr = ksort($data);
    foreach ($data as $key => $row) {
        print $row."<br />\r\n";
    }
} elseif ($type == 'goodlist') {
    sort($idList, SORT_NUMERIC);
    foreach ($idList as $key => $row) {
        print $row."<br />\r\n";
    }
} else {
    if ($type == 'order') {
        $arr = ksort($nameList);
    } else {
        array_multisort($names, SORT_ASC, SORT_STRING, $nameList);
    }

    print "<font size=+2>Item Listing</font>";

    foreach ($nameList as $key => $row) {
        print "\r\n<br />$row";
    }
}
?>
