<?php

/**
  Plugin Name: Font Bird
  Plugin URI: http://axcoto.com
  Description: Manage your font and create custom style for site's font
  Version: 1.0
  Author: kureikain <info@axcoto.com>
  Author URI: http://axcoto.com/blog
 */
include 'helper/util.php';
include 'helper/font.php';
include 'controller/base.php';

class AxcotoFontBird {

    public function __construct() {
        $this->pluginName = dirname(plugin_basename(__FILE__));
        $this->pluginUrl = WP_PLUGIN_URL . "/$this->pluginName/";
        $this->pluginPath = WP_PLUGIN_DIR . "/$this->pluginName/";

        wp_register_style('fontbird-all', $this->pluginUrl . '/assets/css/axcoto.css', false, '2.0.1');
        wp_register_style('fontbird-uploadify', $this->pluginUrl . '/assets/js/uploadify/uploadify.css', false, '2.0.1');

        $fonts = AxcotoFontBirdFontHelper::getFonts();
        $fonts = implode(';', $fonts);
        wp_register_style('fontbird-custom-font-admin', $this->pluginUrl . '/font.php?fonts=' . $fonts, false, '2.0.1');
        wp_register_style('fontbird-custom-font-fron', $this->pluginUrl . '/assets/css/fontbird.css', false, '2.0.1');


        wp_register_script('fontbird-swfobject', $this->pluginUrl . '/assets/js/uploadify/swfobject.js');
        wp_register_script('fontbird-uploadify', $this->pluginUrl . '/assets/js/uploadify/jquery.uploadify.v2.1.4.min.js');
        wp_register_script('fontbird-app', $this->pluginUrl . '/assets/js/app.js');

        register_activation_hook(__FILE__, array(&$this, 'active'));
        register_deactivation_hook(__FILE__, array(&$this, 'deactive'));


        add_action('admin_menu', array(&$this, 'menu'));

        if (is_admin ()) {
            wp_enqueue_style(array('fontbird-all', 'fontbird-uploadify', 'fontbird-custom-font-admin'));
            wp_enqueue_script('fontbird-swfobject');
            wp_enqueue_script('fontbird-uploadify');
            wp_enqueue_script('fontbird-app');
        } else {
            wp_enqueue_style(array('fontbird-custom-font-fron'));
        }
        add_action('init', array(&$this, 'init'));
    }

    /**
     * Main method to router and execute request in wordpress back-end!
     * It router request, dispatch, load controller then return responding
     *
     * @see handleBackendAction
     */
    public function execute() {
        $this->router = array();
        $this->router['controller'] = 'front';
        $this->router['method'] = 'index';

        $this->router['segment'] = empty($_GET['uri']) ? (empty($_GET['page']) ? 'front' : $_GET['page']) : $_GET['uri'];

        $part = explode('/', $this->router['segment']);

        $segmentCount = count($part);

        if ($segmentCount > 2) {
            $this->router['controller'] = $part[1];
            $this->router['method'] = $part[2];
            ;
            $param = array_slice($part, 3);
        } elseif ($segmentCount == 2) {
            $this->router['controller'] = $part[1];
            $this->router['method'] = 'index';
            $param = array();
        } else {
            $this->router['controller'] = 'front';
            $this->router['method'] = 'index';
            $param = array();
        }

        if (!file_exists($this->pluginPath . '/controller/' . $this->router['controller'] . '.php')) {
            $this->router['controller'] = 'front';
        }
        include $this->pluginPath . 'controller/' . $this->router['controller'] . '.php';
        $className = 'AxcotoFontBird' . ucfirst($this->router['controller']) . 'Controller';

        $this->responser = new $className;

        if (!method_exists($this->responser, 'action_' . $this->router['method'])) {
            $param = array_merge(array($this->router['method']), $param);
            $this->router['method'] = 'index';
        }
        echo call_user_func_array(array(&$this->responser, 'action_' . $this->router['method']), $param);
    }

    /**
     * Handle hook, filter via manually call
     * @param <type> $kk
     */
    public function work($kk) {
        //Handle
        $this->router['controller'] = 'front';
        $this->router['method'] = 'index';
        //Come from URL
        $this->router['segment'] = empty($_GET['kk']) ? 'front' : $_GET['kk'];
        $part = explode('/', $this->router['segment']);

        if ($segmentCount > 2) {
            $router['controller'] = $part[1];
            $router['method'] = $part[2];
        } elseif ($segmentCount == 2) {
            $router['controller'] = $part[1];
            $router['method'] = 'index';
        } else {
            $router['controller'] = 'collection';
            $router['method'] = 'index';
        }

        if (file_exists($this->pluginPath . '/controller/' . $router['controller'] . '.php')) {
            
        } else {
            $router['controller'] = 'front';
        }
        include $this->pluginPath . '/controller/' . $router['controller'] . '.php';
        $className = 'AxcotoFontBird' . $router['controller'] . 'Controller';
        $this->responser = new $className;
        echo call_user_method('action_' . $router['method'], $this->responser);
    }

    /**
     * This is main  entry point for all request come to WordPress backend! It then call execute() to execure requesr
     */
    public function handleBackendAction() {
        $this->execute();
    }

    public function menu() {
        add_menu_page('Font Bird', 'Font Bird', 8, 'fontbird/collection', array(&$this, 'handleBackendAction'), $this->pluginUrl . '/assets/images/icon.png');
        add_submenu_page('fontbird/collection', 'Manage Font', 'Manage font', 8, 'fontbird/font', array(&$this, 'handleBackendAction'));
        add_submenu_page('fontbird/collection', 'Manage Font', 'Add font', 8, 'fontbird/font/new', array(&$this, 'handleBackendAction'));
        add_submenu_page('fontbird/collection', 'Help', 'Help', 8, 'fontbird/page/help', array(&$this, 'handleBackendAction'));
        add_submenu_page('fontbird/collection', 'About', 'About', 8, 'fontbird/page/about', array(&$this, 'handleBackendAction'));
    }

    public function init() {
        
    }

    public function active() {
        global $wpdb;
        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
        $sql = file_get_contents(dirname(__FILE__) . '/install/install.sql');
        $sql = str_replace('axcoto_', $wpdb->prefix, $sql);
        $sql = explode(';', $sql);
        foreach ($sql as $item) {
            dbDelta($item);
        }
    }

    public function deactive() {
        global $wpdb;
        $content = file_get_contents($this->pluginPath . 'install/uninstall.sql');
        $content = str_replace('axcoto_', $wpdb->prefix, $content);
        $content = explode(';', $content);
        foreach ($content as $sql) {
            $wpdb->query(trim($sql));
        }
    }

    public function getOption() {
        
    }

    static public function singleton() {
        if (!self::$_self) {
            self::$_self = new AxcotoFontBird();
        }
        return self::$_self;
    }

    public function uri($page, $uri) {
        return 'admin.php?page=fontbird/' . $page . '&uri=fontbird/' . $uri;
    }

    static private $_self = null;
    const VERSION = '1.1';
    public $optionName = 'axcoto_fontbird';
    public $pluginUrl = NULL;
    public $pluginPath = '';
    public $pluginName = '';
    public $router = array();

}

AxcotoFontBird::singleton();