<?php
    $id = null;
    $content = file_get_contents(dirname(__FILE__).'/saved_slideshow.json');
    $content = json_decode($content);

    $htmlContent = file_get_contents(dirname(__FILE__).'/saved_slideshow_html.json');
    $htmlContent = json_decode($htmlContent);

    // debug_log(print_r($content2, true));

    if (!$content || $content == '') {
        $content = array();
        $htmlContent = array();
    }

    if (null != $_POST['id'] && count($content) - 1 > $_POST['id']) {
        $_POST['id'] = 'null';
    }

    if ('null' == $_POST['id'] || null == $_POST['id']) {      
        $content[] = json_decode($_POST['value']);
        $htmlContent[] = json_decode($_POST['html']);
        $id = count($content) - 1;
    } else {
        $content[$_POST['id']] = json_decode($_POST['value']);
        $htmlContent[$_POST['id']] = json_decode($_POST['html']);
        $id = $_POST['id'];
    }

    // file_put_contents(dirname(__FILE__).'/test.txt', 'id - '.$id, FILE_APPEND | LOCK_EX);

    // file_put_contents(dirname(__FILE__).'/saved_slideshow.json', print_r($content, true));
    // debug_log(print_r($content, true));
    file_put_contents(dirname(__FILE__).'/saved_slideshow.json', json_encode($content));
    file_put_contents(dirname(__FILE__).'/saved_slideshow_html.json', json_encode($htmlContent));

    echo "{\"id\":\"".$id."\"}";

    // file_put_contents(dirname(__FILE__).'/saved_slideshow.json', print_r($_POST, true));
    // file_put_contents(dirname(__FILE__).'/saved_slideshow.json', print_r($id, true));

    // json_encode()
    // json_decode()
?>