<?php 

/**
 * summary
 */
class template extends app
{
    /**
     * summary
     */
    public function __construct()
    {
        $this->templates = array();
    }

    public function make_placeholder($string) {
        $string = "[placeholder ".$string."]";
        return $string;
    }

    public function get_option($placeholder) {
        $placeholder = str_replace("[placeholder", "", $placeholder);
        $placeholder = str_replace("]", "", $placeholder);
        $placeholder = trim($placeholder);
        $option = get_option($placeholder);
        if(is_string($option)) {
            return $option;
        } else return $this->make_placeholder($placeholder);
    }

    public function filter_template($template) {
        $needle = "[placeholder";
        $startPos = $lastPos = 0;
        $filtered = $template;
        while (($lastPos = strpos($template, $needle, $lastPos))!== false) {
            if($needle == "[placeholder") {
                $startPos = $lastPos;
                $needle = "]";
            } else {
                $stopPos = $lastPos + 1;
                $needle = "[placeholder";
                $len = $stopPos - $startPos;
                $placeholder = substr($template, $startPos, $len);
                $filter = $this->get_option($placeholder);
                $filtered = str_replace($placeholder, $filter, $filtered);
            }
            $lastPos = $lastPos + strlen($needle);
        }
        return $filtered;
    }

    public function decrypt_template($template, $placeholders = array()) {
        if(_count($placeholders)) {
            foreach ($placeholders as $key => $value) {
                $placeholder = $this->make_placeholder($key);
                $template = str_replace($placeholder, $value, $template);
            }
        }
        return $this->filter_template($template);
    }

    public function space_out_placeholder($placeholder) {
        $placeholder = str_replace("[placeholder", "[placeholder ", $placeholder);
        return trim($placeholder);
    }

    public function register_template_position($template_position, $default_template = "default") {
        $template_position = trim(strtolower($template_position));
        $positions = get_option("template-positions");
        if(!_count($positions))
            $positions = array();
        array_push($positions, $template_position);
        array_unique($positions);
        update_option("template-positions", $positions);
        update_option("tmp-default-".$template_position, $default_template);
    }

    public function get_template($template_position, $placeholders = array(), $template_name = false) {
        set_time_limit(60);
        if(!isset($this->templates[$template_position])) {
            $positions = $this->check_template_position($template_position);
            if($positions) {
                $temp = $template_name ? $template_name : get_option("tmp-active-".$template_position);
                $default_temp = get_option("tmp-default-".$template_position);
                if($existing = db_post_unique(['type' => 'template_file', 'template_name' => $temp], 'LIKE', false)) {
                    $template_url = $existing['template_url'];
                } else if($existing = db_post_unique(['type' => 'template_file', 'template_name' => $default_temp], 'LIKE', false)) {
                    $template_url = $existing['template_url'];
                } else $template_url = "app/templates/blank.html";
            } else $template_url = "app/templates/blank.html";
            ob_start();
            require_once(ABSPATH.$template_url);
            $this->templates[$template_position] = ob_get_contents();
            ob_get_clean();
        }
        return $this->decrypt_template($this->templates[$template_position], $placeholders);
    }

    public function check_template_position($template_position) {
        $positions = get_option("template-positions");
        if(!$positions || !in_array($template_position, $positions)) {
            $this->register_template_position($template_position);
        }
        return $positions;
    }

    public function register_template($template_url, $template_name, $template_position) {
        $post = new post;
        $args = [
            "type" => "template_file",
            "template_position" => $template_position,
            "template_name" => $template_name,
            "template_url" => $template_url,
            "status" => "active"
        ];
        if(db_post_unique(["type" => "template_file", "template_position" => $template_position, "template_name" => $template_name])) {
            return;
        } else {
            if(!db_post_unique(["type" => "template_file", "template_position" => $template_position, "status" => "active"])) {
                update_option("tmp-active-".$template_position, $template_name);
            }
            $post->add_post($args);
        }
    }
}