<?php
ini_set("display_errors", "on");
error_reporting(E_ALL ^ E_NOTICE);

$curFile = $_GET['curFile'];
$action = $_GET['action'];
$missionNum = $_GET['missionNum'];
$print = $_GET['print'];
$newIndList = array();
$imTarg = 'Image-O-Matic2.php';

function fileWrite($x){
     $x .= "\r\n";
     global $file;
     global $nameList;
     global $curFile;
     if (!$file) {
         $file = fopen($nameList[$curFile].'Parse.txt', 'w+');
     }
     if (!fwrite($file, $x)){
         print "&errMsg=Unable to print line $x/r<br>";
     }
}

function breakLine($y) {
    $y = str_replace('[br]', ' ', $y);
    $arr = explode('|', $y);
    return ($arr);
}

$nameList = array('', 'BastokMissions', 'SanDorianMissions', 'WindurstMissions', 'ZilartMissions', 'PromathiaMissions', 'AhtUrhganMissions', 'GoddessMissions', 'campaignOp', 'CrystallineMissions', 'KupoMissions', 'ShantottoMissions');
$catList = array('', 'Bastok', "San d'Oria", 'Windurst', 'Zilart', 'Promathia', 'Aht Urhgan', 'Wings of the Goddess', 'CampaignOps', 'Crystalline', "A Moogle Kupo d'Etat", 'A Shantotto Ascension');
$expList = array('', 'Bastok', "San d'Oria", 'Windurst', 'Zilart', 'Promathia', 'Aht Urhgan', '', '', 'Crystalline', 'Kupo', 'A Shantotto Ascension');
//$fameList = array('', 'Bastok', "San d'Oria", 'Windurst', 'Jeuno', 'Other', 'Outlands', 'Treasures of Aht Urhgan', 'Wings of the Goddess');
//$expList = array('', '', '', '', '', '', 'Rise of the Zilart', 'Treasues of Aht Urhgan', 'Wings of the Goddess');

$newData = array();

$printString = ($print == true) ? '&print=true' : '';

if ($curFile == null) {
    print "<b>Listing</b><br />\r\n";
    foreach ($nameList as $key => $curName) {
        if ($key > 0) {
            print "<a href=\"Mission-O-Matic.php?curFile=".$key."&action=list$printString\">".$curName."</a><br />\r\n";
        }
    }
} elseif ($curFile == 8) {
    $array = file($nameList[$curFile].'Parse.txt');
    $data = array();
    $counter = 0;
    //print "action = ".$action."<br />\r\n";
    if (!$array) {
        print ('Unable to open file');
    } elseif ($action == "list") {
        if (strlen($missionNum) > 0 ) {
            $thisLine = breakLine($array[$missionNum]);
            $orders = str_replace('Objective:  ', '', $thisLine[4]);
            $idInt = 0;
            $starter = array();
            $starter[] = array('Rasdinice', "Southern San d'Oria (S)", 'I-9');
            $starter[] = array('Hieronymus', 'Bastok Markets (S)', 'E-8');
            $starter[] = array('Emhi Tchaoryo', 'Windurst Waters (S)', 'H-9');
            if (strstr($thisLine[3], '(B)') !== false) {
                $idInt = 1;
            } elseif (strstr($thisLine[3], '(W)') !== false) {
                $idInt = 2;
            }
            print "<html>
<head><title>".$thisLine[3]."</title>
</head>
<body>
{{Stub|Mission}}<br />
{{Campaign Header<br />
|NPC Name=".$starter[$idInt][0]."<br />
|Zone=".$starter[$idInt][1]."<br />
|pos=".$starter[$idInt][2]."<br />
|Campaign Type=<br />
|Campaign Rank=<br />
|Description=$orders<br />
|Orders=<br />
|Time=<br />
|Level=<br />
|Rewards=<br />
}}
<br />&lt;br /&gt;
<br />
<br />== Walkthrough ==
<br />
</body>
</html>";
        } else {
            $printTarg = ($print == true) ? "Image-O-Matic2.php?&fileSystem=Missions" : "Mission-O-Matic.php?";
            print "Listing Campaign Operations<br /><br />\r\n";
            foreach ($array as $key => $curLine) {
                 $thisLine = breakLine($curLine);
                 if (substr($thisLine[3], 0, 2) == "**") {
                     $thisLine[3] = substr($thisLine[3], 2);
                 }
                 if ($thisLine[3] != '??????????' && $thisLine[3] != 'Campaign Operation') {
                     print "* [[<a href=\"$printTarg&curFile=".$curFile."&action=list&missionNum=".$key."\">".$thisLine[3]."</a>]]<br />\r\n";
                     array_push($newIndList, $thisLine[1]);
                 }
             }
             $indList = implode(',', $newIndList);
             if ($print == true) {
                 print "\r\n<br /><br /><a href=\"$imTarg?&action=batch&fileSystem=".$nameList[$curFile]."&ind=$indList\">Campiagn Op batch image processing</a><br /><br />";
             }
        }
    }
} else {
    $array = file($nameList[$curFile].'Parse.txt');
    $data = array();
    $counter = 0;
    //print "action = ".$action."<br />\r\n";
    if (!$array) {
        print ('Unable to open file');
    } elseif ($action == "list") {
        $missionLinks = array();
        $missionNames = array();
        $linkNum = array();
        $misSet = $expList[$curFile];
        $cityList = array('1-1', '1-2', '1-3', '2-1', '2-2', '2-3', '', '', '', '', '3-1', '3-2', '3-3', '4-1', '5-1', '5-2', '6-1', '6-2', '7-1', '7-2', '8-1', '8-2', '9-1', '9-1');
        if ($misSet == "Bastok" || $misSet == "San d'Oria" || $misSet == "Windurst") {
            foreach($cityList as $c => $num) {
                if (strlen($num) > 0) {
                    array_push($missionLinks, "$misSet Mission $num");
                    $thisLine = breakLine($array[$c]);
                    array_push($missionNames, $thisLine[3]);
                    array_push($linkNum, $c);
                }
            }
        } else {
            foreach($array as $c => $num) {
                $d = $c+1;
                if ($misSet != 'Zilart') {
                    array_push($missionLinks, "$misSet Mission $d");
                }
                $thisLine = breakLine($num);
                $thisLine[3] = str_replace('_', '', $thisLine[3]);
                array_push($missionNames, $thisLine[3]);
                array_push($linkNum, $c);
            }
        }
        if (strlen($missionNum) > 0 ){
            $thisLine = breakLine($array[$missionNum]);
            $misName = $thisLine[3];
            $curNum = array_search($misName, $missionNames);
            $nextLine = (isset($missionNames[$curNum+1])) ? $missionNames[$curNum+1] : '';
            $prevLine = (isset($missionNames[$curNum-1])) ? $missionNames[$curNum-1] : '';
            $locCat = "[[Category:".$catList[$curFile]." Missions]] ";
            $joiner = ($misSet != "Zilart") ? '{{!}}' : '';
            $next = (strlen($nextLine) > 0) ? $missionLinks[$curNum+1] . $joiner . $nextLine : '';
            $prev = (strlen($prevLine) > 0) ? $missionLinks[$curNum-1] . $joiner . $prevLine : '';
            print "<html>
<head><title>".$thisLine[3]."</title>
</head>
<body>
{{Stub|Mission}}<br />
{{Mission Header<br />
|Mission Name=".$thisLine[3]."<br />
|Expansion=".$catList[$curFile]."<br />
|Start=<br />
|Description=".$thisLine[4]."<br />
|Level=<br />
|Repeatable=<br />
|Previous=$prev<br />
|Next=$next<br />
|Title=<br />
|Reward=<br />
|Image=<br />
}}
<br />&lt;br /&gt;
<br />
<br />== Walkthrough ==
<br />
<br />$locCat
</body>
</html>";
        } else {
            $printTarg = ($print == true) ? "Image-O-Matic2.php?&fileSystem=Missions" : "Mission-O-Matic.php?";
            print "Listing ".$catList[$curFile]." Missions<br /><br />\r\n";
            foreach ($missionNames as $key => $curLine) {
                 $link = isset($missionLinks[$key]) ? $missionLinks[$key] . "|" : '';
                 $linkBack = $linkNum[$key];
                 if (substr($curLine, 0, 2) == "**") {
                     $curLine = substr($curLine);
                 }
                 print "* [[$link<a href=\"$printTarg&curFile=".$curFile."&action=list&missionNum=$linkBack\">$curLine</a>]]<br />\r\n";
                 array_push($newIndList, $thisLine[1]);
             }
             $indList = implode(',', $newIndList);
             if ($print == true) {
                 print "\r\n<br /><br /><a href=\"$imTarg?&action=batch&fileSystem=".$nameList[$curFile]."&ind=$indList\">Mission batch image processing</a><br /><br />";
             }
        }
    }
}
