<!DOCTYPE html>
<html lang="ru">
    <head>
        <style>
            <? echo file_get_contents(__DIR__ . '/css/styles.css'); ?>
        </style>
        <link href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAABGdBTUEAALGOfPtRkwAAACBjSFJNAAB6JQAAgIMAAPn/AACA5gAAdS4AAOpfAAA6lwAAF29p5MQrAAAAoklEQVR4nGL4z8CAD/1HRwABRLJqgAAiWTVAAJGsGiCAQGg/GtqPjhBy/xkAAgjGQAIMYD1wNgMy+z8DQAAhVDOAAZyNSwNAAKFoQDYYu4YN/wECCKcGBlR3wjUABBC6B5ANhgNkDQABhN1UZJcgywI1AAQQONRgAJmNbANC9Yb/AAFEKC4hipAQQACRqHrDf4AAIlH1hv8AAUSi6g3/AQIMAKWpd2XEJGmSAAAAAElFTkSuQmCC" rel="icon" type="image/png" />
    </head>
    <body>
        <div class="page-header"><img class="page-header__img" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAABGdBTUEAALGOfPtRkwAAACBjSFJNAAB6JQAAgIMAAPn/AACA5gAAdS4AAOpfAAA6lwAAF29p5MQrAAAAoklEQVR4nGL4z8CAD/1HRwABRLJqgAAiWTVAAJGsGiCAQGg/GtqPjhBy/xkAAgjGQAIMYD1wNgMy+z8DQAAhVDOAAZyNSwNAAKFoQDYYu4YN/wECCKcGBlR3wjUABBC6B5ANhgNkDQABhN1UZJcgywI1AAQQONRgAJmNbANC9Yb/AAFEKC4hipAQQACRqHrDf4AAIlH1hv8AAUSi6g3/AQIMAKWpd2XEJGmSAAAAAElFTkSuQmCC">The PSG Packer v1.0</div>
        <? if (empty($psgFileContent)) { ?>
            <div class="center-wrap">
                <div class="center">
                    <div class="block">
                        <div class="block__hdr">Upload PGS file</div>
                        <div class="block__content">
                            <form action="index.php" method="post" enctype="multipart/form-data">
                                <table>
                                    <tr>
                                        <td>PSG file:</td>
                                        <td><input type="file" name="psgfile" accept=".psg" /></td>
                                    </tr><tr>
                                        <td>PSG map file:</td>
                                        <td><input type="file" name="psgmapfile" /></td>
                                    </tr><tr>
                                        <td></td>
                                        <td><br><button>UPLOAD</button></td>
                                    </tr>
                                </table>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        <? } else {?>
            <form action="index.php" method="post" enctype="multipart/form-data">
                <input type="hidden" name="apply" value="1">
                <input type="hidden" name="psgfile" value="<?= (base64_encode($psgFileContent)) ?>">
                <input type="hidden" name="psgmapfile" value="<?= (base64_encode($psgMapFileContent)) ?>">
				<input type="hidden" name="psgFileName" value="<?= $psgFileName ?>">
				
				<table>
					<tr valign="top">
						<td>
							<div class="block">
								<div class="block__hdr">Info</div>
								<div class="block__content">
									<table>
										<tr>
											<td>filename:</td><td><?= $psgFileName ?></td>
										</tr><tr>
											<td>time:</td><td><?= $timeMin ?>m <?= $timeSec ?>sec</td>
										</tr><tr>
											<td>frames:</td><td><?= number_format(sizeof($dump), 0, '.', '`') ?></td>
										</tr><tr>
											<td>original psg size:</td><td><?= number_format(strlen($psgFileContent), 0, '.', '`') ?> bytes</td>
										</tr><tr>
											<td>clean psg size:</td><td><?= number_format(strlen($psgCleanFileContent), 0, '.', '`') ?> bytes</td>
										</tr><tr>
											<td>compact psg size:</td><td><?= number_format(strlen($result->resultRawStream), 0, '.', '`') ?> bytes</td>                                
										</tr><tr>
											<td>compressed size:</td><td>
												<?= number_format(strlen($result->resultStream), 0, '.', '`') ?> bytes
												<? if (strlen($result->resultStream) > 16384) { ?>
													- warning, size > 16384 - only relative calls allowed
												<? } ?>
											</td>
										</tr><tr>
											<td>repeats qty:</td><td><?= $result->totalRepeatsQty ?></td>
										</tr><tr>                            
											<td>max repeats size:</td><td><?= $result->maxRepeatsQty ?></td>
										</tr>
										<? if (isset($mask) && $mask->maskIndexIsBad) { ?>
											<tr><td colspan="100%">index mask not effective, disabled</td></tr>
										<? } elseif ($flags->indexed_mask) { ?>
											<tr>
												<td>index mask count:</td><td><?= sizeof($mask->maskIndex) ?></td>
											</tr><tr>
												<td>index mask econom:</td><td><?= ($noMaskSize - strlen($result->resultStream)) ?></td>
											</tr>
										<? } ?>
										<tr>
											<td>max call loop:</td><td><?= $analyze->maxCallLoop ?></td>
										</tr><tr>
											<td>max call loop ret:</td><td><?= $analyze->maxCallLoopRet ?></td>
										</tr><tr>
											<td>partial call count:</td><td><?= $analyze->partialCallCount ?></td>
										</tr><tr>
											<td>call to call count:</td><td><?= $analyze->callToCallCount ?></td>   
										</tr><tr>
											<td>compile time:</td><td><?= number_format($time, 4, '.', '') ?> sec</td>
										</tr><tr>
											<td>memory used:</td><td><?= number_format(memory_get_peak_usage(true), 0, '.', '`') ?> bytes</td>
											
										</tr>
									</table>
								</div>
							</div>
						</td>
						<td>
							<? if ($execMode == EXEC_MODE_WWW) { ?>
								<div class="block">
									<div class="block__hdr">Settings</div>
									<input type="hidden" name="apply_settings" value="1">
									<div class="block__content">
										<table>
											<tr valign="top">
												<td>
													<div class="left">
														<div>module adr: <input type="text" name="module_adr" value="<?= $flags->module_adr ?>"></div>
														<? if ($flags->module_adr + strlen($result->resultStream) > 65535) { ?>
															<div style="color:#ff0000;">error, must be <= <?= (65536 - strlen($result->resultStream)) ?></div>
														<? } ?>
														<div><label><input value="1" type="checkbox" name="loop" <?= $flags->loop ? 'checked' : '' ?>> loop</label></div>
														<div><label><input value="1" type="checkbox" name="indexed_mask" <?= $flags->indexed_mask ? 'checked' : '' ?>> use index mask</label></div>
														<div><label><input value="1" type="checkbox" name="indexed_mask_static_buff" <?= $flags->indexed_mask_static_buff ? 'checked' : '' ?>> use static size for indexed mask buff</label></div>
														<div><label><input value="1" type="checkbox" name="allow_call_to_call" <?= $flags->allow_call_to_call ? 'checked' : '' ?>> allow call to call</label></div>
														<div><label><input value="1" type="checkbox" name="relative_call" <?= $flags->relative_call ? 'checked' : '' ?>> relative call</label></div>
														<div><label><input value="1" type="checkbox" name="init_first_frame" <?= $flags->init_first_frame ? 'checked' : '' ?>> init first frame</label></div>
														<hr>
														<div><label><input value="1" type="checkbox" name="clean_dump" <?= $flags->clean_dump ? 'checked' : '' ?>> clean source dump</label></div>
														<div><label><input value="1" type="checkbox" name="clean_tone_a" <?= $flags->clean_tone_a ? 'checked' : '' ?>> clean tone a</label></div>
														<div><label><input value="1" type="checkbox" name="clean_tone_b" <?= $flags->clean_tone_b ? 'checked' : '' ?>> clean tone b</label></div>
														<div><label><input value="1" type="checkbox" name="clean_tone_c" <?= $flags->clean_tone_c ? 'checked' : '' ?>> clean tone c</label></div>
														<div><label><input value="1" type="checkbox" name="clean_noise" <?= $flags->clean_noise ? 'checked' : '' ?>> clean noise</label></div>
														<div><label><input value="1" type="checkbox" name="clean_env" <?= $flags->clean_env ? 'checked' : '' ?>> clean envelope tone</label></div>
														<div><label><input value="1" type="checkbox" name="clean_env_form" <?= $flags->clean_env_form ? 'checked' : '' ?>> clean envelope form</label></div>
													</div>
													<div class="clear"></div>
												</td>
												<td>
													<div><label><input value="1" type="checkbox" name="loss_noise" <?= $flags->loss_noise ? 'checked' : '' ?>> loss noise</label></div>
													<? renderLossVol($flags, $stat, 'a'); renderLossVol($flags, $stat, 'b'); renderLossVol($flags, $stat, 'c'); ?>
													<div class="clear"></div>
												</td>
											</tr>
										</table>

									</div>
								</div>
							<? } ?>
						</td>
					</tr>
				</table>
                <div class="block">
                    <div class="block__hdr">Compiled diagram</div>
                    <div class="block__content">
                        <div class="canvas-wrap">
                            <canvas class="canvas" id="canvas" style="background:#000;"></canvas>
                            <div class="canvas__info" id="canvas-info"></div>
                            <div class="canvas__cursor-v" id="canvas-cursor-v"></div>
                            <div class="canvas__play-cursor" id="canvas-play-cursor"></div>
                        </div>
                        <div style="padding-top:10px">
                            <button id="play-start"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAALGOfPtRkwAAACBjSFJNAAB6JQAAgIMAAPn/AACA5gAAdS4AAOpfAAA6lwAAF29p5MQrAAAAa0lEQVR4nGJgoBAABBDFCCCAKEYAAYQN/YdiZD5OABBAxBqA0xCAACLFAKyGAAQQqQZgGAIQQOQYgGIIQABRbABAAJFjAAoACCBSDcAAAAFEigFYAUAAEWsATgAQQBQjgACiGAEEEMUIIMAA/ZMxzxhN7JAAAAAASUVORK5CYII="></button>
                            <button id="play"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAALGOfPtRkwAAACBjSFJNAAB6JQAAgIMAAPn/AACA5gAAdS4AAOpfAAA6lwAAF29p5MQrAAAAaUlEQVR4nGJgoBAABBDFCCCAKEYAAYQP/SdGEUAAETKAoCEAAUSMAXgNAQggYg3AaQhAAJFiAFZDAAKIYgMAAogUA7ACgAAi1gCcACCAiDEALwAIIEIGEAQAAUQxAgggihFAAFGMAAIMAKG9HeO93M4KAAAAAElFTkSuQmCC"></button>
                            <button id="stop"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAALGOfPtRkwAAACBjSFJNAAB6JQAAgIMAAPn/AACA5gAAdS4AAOpfAAA6lwAAF29p5MQrAAAAWElEQVR4nGJgoBAABBDFCCCAKEYAAUQxAgggbOg/AYwCAAKIYgMAAohiAwACiGIDAAKIYgMAAohiAwACiGIDAAKIYgMAAohiBBBAFCOAAKIYAQQQxQggwAAp/z/BsOzA4QAAAABJRU5ErkJggg=="></button>
                        </div>
                    </div>
                </div>

                <? if ($execMode == EXEC_MODE_WWW) { ?>
                    <div style="padding:16px;">
                        <button>RECOMPILE</button>
						<span style="margin-left:20px;">
							<button name="action" value="downloadClean">DOWNLOAD CLEAN</button>
							<button name="action" value="downloadPacked">DOWNLOAD PACKED</button>
						</span>
                    </div>
                <? } ?>

            </form>
        <? } ?>
		
		<? if (isset($analyze) && $analyze) { ?>
			<div class="block">
				<div class="block__hdr">Analyse</div>
				<div class="block__content">
					<table>
						
						
						<tr><td>psg1Count: </td><td><?= $analyze->psg1Count ?></td></tr>
						<tr><td>psg2Count: </td><td><?= $analyze->psg2Count ?></td></tr>
						<tr><td>psg2iCount: </td><td><?= $analyze->psg2iCount ?></td></tr>
						<tr><td>pause1Count: </td><td><?= $analyze->pause1Count ?></td></tr>
						<tr><td>pauseNCount: </td><td><?= $analyze->pauseNCount ?></td></tr>
						<tr><td>call1Count: </td><td><?= $analyze->call1Count ?></td></tr>
						<tr><td>callNCount: </td><td><?= $analyze->callNCount ?></td></tr>
						<tr valign="top">
							<td>playPSG2Sizes: </td>
							<td>
								<? foreach ($result->playPSG2Sizes as $k => $v) { ?>
									<div><?= $k ?> => <?= $v ?></div>
								<? } ?>
							</td>
						</tr>
						<tr valign="top">
							<td>playPSGiSizes: </td>
							<td>
								<? foreach ($result->playPSGiSizes as $k => $v) { ?>
									<div><?= $k ?> => <?= $v ?></div>
								<? } ?>
							</td>
						</tr>
						<tr valign="top">
							<td>changeRegs: </td>
							<td>
								<? foreach ($result->changeRegs as $k => $v) { ?>
									<div><?= $k ?> => <?= $v ?></div>
								<? } ?>
							</td>
						</tr>
					</table>
				</div>
			</div>
		<? } ?>
    </body>
</html>

<script type="text/javascript">
<? 
if (!empty($psgFileContent)) {
    echo file_get_contents(__DIR__ . '/js/ayumi.js');
    echo file_get_contents(__DIR__ . '/js/ayumi-player.js');
    include(__DIR__ . '/js/script.php');
}
?>
</script>
<?
function renderLossVol($flags, $stat, $regName) {
    $field = 'loss_volume_' . $regName;
    $fieldTbl = 'loss_volume_' . $regName . '_table';
    $regIdList = array('a' => 8, 'b' => 9, 'c' => 10);
    $regId = $regIdList[$regName];
    ?>
    <div class="left">
        <div><label><input value="1" type="checkbox" name="<?= $field ?>" <?= $flags->$field ? 'checked' : '' ?>> loss volume <?= $regName ?></label></div>
        <table class="less-volume">
            <tr>
                <th>val</th>
                <th>used</th>
                <th>repl</th>
            </tr>
            <? for ($val = 15; $val >= 0; $val--) { ?>
                <tr>
                    <td><b><?= $val ?></b></td>
                    <td><?= isset($stat->changeRegsVariants[$regId][$val]) ? $stat->changeRegsVariants[$regId][$val] : '' ?></td>
                    <td>
                        <? if ($val != 0) { ?>
                            <input name="<?= $fieldTbl ?>[<?= $val ?>]" type="text" style="width:20px" value="<?= $flags->{$fieldTbl}[$val] ?>">
                        <? } ?>
                    </td>
                </tr>
            <? } ?>
        </table>
    </div>
<? }
