<?php

class AxcotoFontBirdCollectionController extends AxcotoFontBirdBaseController {

    public function __construct() {
        
    }

    public function action_index() {
        global $wpdb;

        if (!empty($_POST['collection_name']) && !wp_verify_nonce('add_collection', 'name')) {
            $sql = "INSERT INTO {$wpdb->prefix}fontbird_collections(name, actived)
                            VALUES (%s, %s)
                ";
            $stmt = $wpdb->prepare($sql, $_POST['collection_name'], '0');
            $wpdb->query($stmt);
        }

        $sql = "SELECT id, name, actived
                        FROM    {$wpdb->prefix}fontbird_collections
                        ORDER BY id DESC";
        $collections = $wpdb->get_results($sql);

        include AxcotoFontBird::singleton()->pluginPath . '/templates/collection/index.php';
    }

    public function action_remove() {
        
    }

    public function _set_active($collectionId) {
        global $wpdb;
        $rules = AxcotoFontBirdFontHelper::loadRules($collectionId);
        $fonts = array();
        foreach ($rules as $rule) {
            //$fonts[] = array('id' => $rule->font_id, 'name' => $rule->name, 'slug' => $rule->slug);
            $fonts[] = $rule->name;
        }
        $fonts = implode(';', $fonts);
        ob_start();
        include AxcotoFontBird::singleton()->pluginPath . '/templates/collection/rule.php';
        $content = ob_get_clean();
        file_put_contents(AxcotoFontBird::singleton()->pluginPath . '/assets/css/fontbird.css', $content);
        $sql = "UPDATE {$wpdb->prefix}fontbird_collections
                                SET actived=0
                                WHERE actived=1";
        $wpdb->query($sql);

        $sql = "UPDATE {$wpdb->prefix}fontbird_collections
                                SET actived=1
                                WHERE id=%d";
        $stmt = $wpdb->prepare($sql, $collectionId);
        $wpdb->query($stmt);
    }

    public function action_active($collectionId) {
        $this->_set_active($collectionId);
        AxcotoFontBirdUtil::redirect('Success', 'The collection is made actived!', 'collection', 'collection/open/' . $collectionId);
    }

    public function _is_active($collectionId) {
        global $wpdb;
        $sql = "SELECT count(*) as total
                                FROM {$wpdb->prefix}fontbird_collections
                                WHERE id=%d AND actived =1";
        $stmt = $wpdb->prepare($sql, $collectionId);
        $result = $wpdb->get_var($stmt);
        return $result>=1;
    }

    public function action_open($collectionId) {
        global $wpdb;

        if (!empty($_POST['actived']) && !empty($_POST['collection_id'])) {
            $this->_set_active($_POST['collection_id']);
        }
        if (!empty($_POST['selector'])) {
            $sql = "INSERT INTO {$wpdb->prefix}fontbird_rules (selector, font_size, collection_id, font_id)
                            VALUES (%s, %s, %d, %d)";
            $stmtRules = $wpdb->prepare($sql, $_POST['selector'], $_POST['font_size'], $_POST['collection_id'], $_POST['font_id']);
            $wpdb->query($stmtRules);

             $this->_is_active($_POST['collection_id'])   && $this->_set_active($_POST['collection_id']);
            
        }


        if (!empty($_POST['add']) && !empty($_POST['collection_name'])) {
            $sql = "UPDATE {$wpdb->prefix}fontbird_collections
                            SET name=%s
                            WHERE id=%d";
            $stmt = $wpdb->prepare($sql, $_POST['collection_name'], $_POST['collection_id']);
            $wpdb->query($stmt);
        }

        $sql = "SELECT id, name, actived
                        FROM {$wpdb->prefix}fontbird_collections
                        WHERE id=%d";
        $stmt = $wpdb->prepare($sql, $collectionId);
        $collection = $wpdb->get_row($stmt);

        $rules = AxcotoFontBirdFontHelper::loadRules($collectionId);

        $fonts = AxcotoFontBirdFontHelper::loadFonts();

        include AxcotoFontBird::singleton()->pluginPath . '/templates/collection/open.php';
    }

    public function action_removeRule($ruleId) {
        global $wpdb;
        $sql = "SELECT collection_id FROM {$wpdb->prefix}fontbird_rules
                        WHERE id=%d";
        $stmt = $wpdb->prepare($sql, $ruleId);
        $collectionId = $wpdb->get_var($stmt);

        $sql = "DELETE FROM {$wpdb->prefix}fontbird_rules
                        WHERE id=%d";
        $stmt = $wpdb->prepare($sql, $ruleId);
        $result = $wpdb->query($stmt);

        $this->_is_active($_POST['collection_id'])   &&  $this->_set_active($collectionId);

        AxcotoFontBirdUtil::redirect('Action Succeed', 'Rule is removed', 'collection', 'collection/open/' . $collectionId);
    }

    public function action_removeCollection($collectionId) {
        global $wpdb;
        $sql = "DELETE FROM {$wpdb->prefix}fontbird_rules
                        WHERE collection_id=%d";
        $stmt = $wpdb->prepare($sql, $collectionId);
        $wpdb->query($stmt);
        $sql = "DELETE FROM {$wpdb->prefix}fontbird_collections
                        WHERE id=%d";
        $stmt = $wpdb->prepare($sql, $collectionId);
        $wpdb->query($stmt);
        AxcotoFontBirdUtil::redirect('Action Succeed', 'Collection is removed', 'collection', 'collection/index');
    }

}
