<?php

namespace Sowmedia\Repo\Frontend;

class LicensePage {
    private static $instance;

    private $inputs;
    private $states;

    private $form_notices;

    function __construct(){
        $this->inputs = array();
        $this->states = array();
        $this->form_notices = new AdminNotices();
        $this->add_hooks();
    }

    private function add_hooks(){
        if(is_admin())
            add_action("admin_menu", array($this, "add_to_menu"));
    }

    function shown(){
        return isset($_GET["page"]) && $_GET["page"] == "sowmedia-licenses";
    }

    function add_to_menu(){
        add_submenu_page("options-general.php", "Sowmedia Licenses", "Sowmedia Plugins", "administrator", "sowmedia-licenses", array($this, "render"));
    }

    function get_url(){
        return admin_url("options-general.php?page=sowmedia-licenses");
    }

    function add_key_input($input){
        $this->inputs[sanitize_title($input["name"])] = $input;
    }

    private function handle_post_data(){
        foreach($this->inputs as $k => &$input){
            if(!isset($_POST[$k]))
                continue;
            $key = trim($_POST[$k]);
            list($ok, $error) = call_user_func($input["do_check"], $key);
            $this->states[$k] = $ok;
            if($ok){
                $input["key"] = $key;
                call_user_func($input["do_save"], $key);
                $this->form_notices->add_notice(sprintf("Succesfully updated the license key for <b>%s</b>", $input["name"]));
            }else $this->form_notices->add_notice(sprintf("<b>Error</b> while updating the license key for <b>%s</b>: <i>%s</i>", $input["name"], htmlspecialchars($error)), "error");
        }
    }

    private function set_states(){
        foreach($this->inputs as $k => $input){
            if(isset($this->states[$k]))
                continue;
            list($ok, $error) = call_user_func($input["do_check"], $input["key"]);
            $this->states[$k] = $ok;
        }
    }

    function render(){
        $this->handle_post_data();
        $this->set_states();

        ?>
        <div class="wrap">
            <h1>Sowmedia Licenses</h1>
            <?php echo $this->form_notices->render_notices(); ?>
            <form name="form" method="post" action="<?php print($this->get_url()); ?>">
                <table class="form-table" style="width:auto">
                    <tbody>
                    <?php foreach($this->inputs as $k => $input){ ?>
                    	<tr>
                    		<th><label for="<?php print($k); ?>"><?php print($input["name"]); ?></label></th>
                    		<td><input name="<?php print($k); ?>" value="<?php print($input["key"]); ?>" class="regular-text code" type="text" <?php if(!$input["enabled"]) print("disabled"); ?> /></td>
                            <td>
                                <span class="dashicons dashicons-<?php $this->states[$k] ? print("yes") : print("no"); ?>" style="color:<?php $this->states[$k] ? print("green") : print("red"); ?>"></span>
                            </td>
                    	</tr>
                    <?php } ?>
                    <tbody>
                </table>
                <p class="submit">
                        <input name="submit" id="submit" class="button button-primary" value="Save changes" type="submit">
                </p>
            </form>
        </div>
        <?php
    }

    static function get(){
        if(self::$instance == null)
            self::$instance = new LicensePage();
        return self::$instance;
    }

    static function setup(){
        // Make sure hooks are added
        self::get();
    }
}
