<?php

namespace PGMB;

use PGMB\Configuration;
use PGMB\DependencyInjection\Container;
use PGMB\EventManagement\EventManager;
use WP_Site;
class Plugin {
    const DOMAIN = 'post-to-google-my-business';

    const VERSION = '3.3.1';

    const DASHICON = 'data:image/svg+xml;base64, ';

    protected $container;

    private $loaded;

    public function __construct( $file, \Freemius $freemius ) {
        $this->container = new Container([
            'plugin_basename'      => plugin_basename( $file ),
            'plugin_domain'        => self::DOMAIN,
            'plugin_path'          => plugin_dir_path( $file ),
            'plugin_relative_path' => basename( plugin_dir_path( $file ) ),
            'plugin_url'           => plugin_dir_url( $file ),
            'plugin_version'       => self::VERSION,
            'plugin_dashicon'      => self::DASHICON,
            'freemius'             => $freemius,
        ]);
        $this->loaded = false;
    }

    public function is_loaded() {
        return $this->loaded;
    }

    public function init() {
        if ( $this->is_loaded() ) {
            return;
        }
        $this->container->configure( [
            Configuration\SettingsConfiguration::class,
            Configuration\ProxyAPIConfiguration::class,
            Configuration\EventManagementConfiguration::class,
            Configuration\NotificationManagerConfiguration::class,
            Configuration\AdminConfiguration::class,
            Configuration\BackgroundProcessConfiguration::class,
            Configuration\PostTypeConfiguration::class,
            Configuration\MetaboxConfiguration::class,
            Configuration\ComponentConfiguration::class
        ] );
        foreach ( $this->container['subscribers'] as $subscriber ) {
            $this->container['event_manager']->add_subscriber( $subscriber );
        }
        $this->load_textdomain();
        mbp_fs()->add_filter( 'plugin_icon', [$this, 'fs_custom_icon'] );
        $this->register_image_sizes();
        $this->loaded = true;
    }

    public static function activate( $network_active ) {
        if ( $network_active ) {
            $site_ids = get_sites( [
                'fields'     => 'ids',
                'network_id' => get_current_network_id(),
            ] );
            foreach ( $site_ids as $site_id ) {
                switch_to_blog( $site_id );
                self::activate_single_site();
                restore_current_blog();
            }
            return;
        }
        self::activate_single_site();
    }

    public static function activate_single_site( $upgrade = false ) {
        if ( !$upgrade ) {
            $created = add_option( 'pgmb_activated', 1 );
        }
        self::configure_roles_caps();
        self::register_database_tables();
    }

    public static function deactivate( $network_active ) {
        if ( $network_active ) {
            $site_ids = get_sites( [
                'fields'     => 'ids',
                'network_id' => get_current_network_id(),
            ] );
            foreach ( $site_ids as $site_id ) {
                switch_to_blog( $site_id );
                self::deactivate_single_site();
                restore_current_blog();
            }
            return;
        }
        self::deactivate_single_site();
    }

    public static function deactivate_single_site() {
        global $wpdb;
        $wpdb->query( "\n\t\t\tDELETE FROM {$wpdb->options} \n\t\t\tWHERE option_name LIKE '_transient_timeout_mbp_%' \n\t\t\tOR option_name LIKE '_transient_mbp_%'\n\t\t\tOR option_name LIKE '_transient_pgmb_%'\n\t\t\tOR option_name LIKE '_transient_timeout_pgmb_%'\n\t\t" );
        if ( function_exists( 'wp_unschedule_hook' ) ) {
            wp_unschedule_hook( 'pgmb_do_evergreen_post' );
        }
    }

    public static function uninstall() {
        if ( function_exists( 'is_multisite' ) && is_multisite() ) {
            return;
        }
        self::uninstall_single_site();
    }

    public static function uninstall_single_site() {
        global $wpdb;
        $settings = get_option( 'mbp_misc' );
        if ( !isset( $settings['uninstall_cleanup'] ) || !is_array( $settings['uninstall_cleanup'] ) ) {
            return;
        }
        $groups_table = $wpdb->prefix . 'pgmb_group_cache';
        $locations_table = $wpdb->prefix . 'pgmb_location_cache';
        $wpdb->query( "DROP TABLE IF EXISTS {$locations_table};" );
        $wpdb->query( "DROP TABLE IF EXISTS {$groups_table};" );
        if ( in_array( 'delete_settings', $settings['uninstall_cleanup'] ) ) {
            global $wpdb;
            $wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE 'mbp_%' OR option_name LIKE 'pgmb_%'" );
        }
        if ( in_array( 'delete_posttypes', $settings['uninstall_cleanup'] ) ) {
            $posts = get_posts( [
                'post_type'      => ['mbp_post_entity', 'mbp-google-subposts', 'mbp-google-posts'],
                'posts_per_page' => -1,
                'fields'         => 'ids',
            ] );
            foreach ( $posts as $id ) {
                wp_delete_post( $id, true );
            }
        }
        //		if(in_array('delete_metadata', $settings['uninstall_cleanup'])){
        //			$meta_keys = [
        //				'mbp_autopost_created',
        //				'_mbp_gutenberg_autopost',
        //				'_pgmb_queued_items',
        //				'mbp_last_error',
        //				'_mbp_autopost_template',
        //				'_pgmb_autopost_template',
        //				'_pgmb_is_evergreen',
        //				'_pgmb_ap_template_id',
        //
        //			];
        //			foreach($meta_keys as $key){
        //				delete_post_meta_by_key($key);
        //			}
        //		}
    }

    public static function insert_site( $blog_id_or_site ) {
        if ( $blog_id_or_site instanceof WP_Site ) {
            $site_id = $blog_id_or_site->id;
        } elseif ( is_int( $blog_id_or_site ) ) {
            $site_id = $blog_id_or_site;
        } else {
            return;
        }
        switch_to_blog( $site_id );
        self::activate_single_site();
        restore_current_blog();
    }

    public function load_textdomain() {
        $dir = $this->container['plugin_relative_path'] . '/languages/';
        load_plugin_textdomain( 'post-to-google-my-business', false, $dir );
    }

    public function fs_custom_icon() {
        return dirname( __FILE__ ) . '/../img/plugin-icon.png';
    }

    public static function configure_roles_caps() {
        //if(get_option('pgmb_roles_configured')){ return; }
        $administrator_role = get_role( 'administrator' );
        if ( $administrator_role instanceof \WP_Role ) {
            $administrator_role->add_cap( 'pgmb_manage_google_accounts' );
            $administrator_role->add_cap( 'pgmb_see_others_accounts' );
        }
        //update_option('pgmb_roles_configured', true);
        /*
         * additional roles to be considered + permission management screen
         *
         * delete_pgmb_posts
         * publish_pgmb_posts
         * edit_pgmb_posts
         * edit_others_pgmb_posts
         * edit_published_pgmb_posts
         * delete_published_pgmb_posts
         */
    }

    public static function premium_version_activation() {
    }

    public static function free_version_reactivation() {
        remove_role( 'pgmb_agency_client' );
    }

    public function register_image_sizes() {
        add_image_size(
            'pgmb-post-image',
            1200,
            667,
            true
        );
    }

    public static function gbp_topic_types() {
        return array(
            'STANDARD' => array(
                'name'     => __( 'Update', 'post-to-google-my-business' ),
                'dashicon' => 'dashicons-megaphone',
                'svg'      => '<svg width="20" height="20" viewBox="0 0 24 24" focusable="false"><path d="M23 12l-2.44-2.78.34-3.68-3.61-.82-1.89-3.18L12 3 8.6 1.54 6.71 4.72l-3.61.81.34 3.68L1 12l2.44 2.78-.34 3.69 3.61.82 1.89 3.18L12 21l3.4 1.46 1.89-3.18 3.61-.82-.34-3.68L23 12zm-4.51 2.11l.26 2.79-2.74.62-1.43 2.41L12 18.82l-2.58 1.11-1.43-2.41-2.74-.62.26-2.8L3.66 12l1.85-2.12-.26-2.78 2.74-.61 1.43-2.41L12 5.18l2.58-1.11 1.43 2.41 2.74.62-.26 2.79L20.34 12l-1.85 2.11z"></path><path d="M11 15h2v2h-2zm0-8h2v6h-2z"></path></svg>',
            ),
            'EVENT'    => array(
                'name'     => __( 'Event', 'post-to-google-my-business' ),
                'dashicon' => 'dashicons-calendar',
                'svg'      => '<svg width="20" height="20" viewBox="0 0 24 24" focusable="false"><path d="M19 4h-1V2h-2v2H8V2H6v2H5c-1.11 0-1.99.9-1.99 2L3 20a2 2 0 0 0 2 2h14c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2zm0 16H5V10h14v10z"></path><path d="M14.5 13a2.5 2.5 0 0 0 0 5 2.5 2.5 0 0 0 0-5z"></path></svg>',
            ),
            'OFFER'    => array(
                'name'     => __( 'Offer', 'post-to-google-my-business' ),
                'dashicon' => 'dashicons-tag',
                'svg'      => '<svg width="20" height="20" viewBox="0 0 24 24" focusable="false"><path d="M21.41 11.58l-9-9C12.05 2.22 11.55 2 11 2H4c-1.1 0-2 .9-2 2v7c0 .55.22 1.05.59 1.42l9 9c.36.36.86.58 1.41.58s1.05-.22 1.41-.59l7-7c.37-.36.59-.86.59-1.41s-.23-1.06-.59-1.42zM13 20.01L4 11V4h7v-.01l9 9-7 7.02zM8 6.5C8 7.33 7.33 8 6.5 8S5 7.33 5 6.5 5.67 5 6.5 5 8 5.67 8 6.5z"></path></svg>',
            ),
            'PRODUCT'  => array(
                'name'     => __( 'Product', 'post-to-google-my-business' ),
                'dashicon' => 'dashicons-cart',
                'svg'      => '<svg height="20" viewBox="0 0 24 24" width="20" focusable="false"><path d="M18,6h-2c0-2.21-1.79-4-4-4S8,3.79,8,6H6C4.9,6,4,6.9,4,8v12c0,1.1,0.9,2,2,2h12c1.1,0,2-0.9,2-2V8C20,6.9,19.1,6,18,6z M12,4c1.1,0,2,0.9,2,2h-4C10,4.9,10.9,4,12,4z M18,20H6V8h2v2c0,0.55,0.45,1,1,1s1-0.45,1-1V8h4v2c0,0.55,0.45,1,1,1s1-0.45,1-1V8 h2V20z"></path></svg>',
            ),
            'ALERT'    => [
                'name'     => __( 'Alert', 'post-to-google-my-business' ),
                'dashicon' => 'dashicons-sos',
                'svg'      => '<span class="dashicons dashicons-sos"></span>',
            ],
        );
    }

    public static function register_database_tables() {
        require_once ABSPATH . 'wp-admin/includes/upgrade.php';
        global $wpdb;
        $groups_table = $wpdb->prefix . 'pgmb_group_cache';
        $locations_table = $wpdb->prefix . 'pgmb_location_cache';
        $charset_collate = $wpdb->get_charset_collate();
        $sql = "CREATE TABLE {$groups_table} (\n    \t\tid bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,\n    \t\taccount_id varchar(255) NOT NULL,\n    \t\tgoogle_id varchar(100) NOT NULL,\n    \t\tgroup_name varchar(100) NOT NULL,\n    \t\timported_at datetime NOT NULL,\n    \t\tin_latest_import tinyint(1) NOT NULL DEFAULT 1,\n    \t\tPRIMARY KEY  (id),\n    \t\tUNIQUE KEY `google_id` (`google_id`)\n\t\t) {$charset_collate};";
        $sql .= "CREATE TABLE {$locations_table} (\n    \t\tid bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,\n    \t\tgroup_id bigint(20) UNSIGNED NOT NULL,\n    \t\tgoogle_id varchar(100) NOT NULL,\n    \t\tstore_code varchar(100) NOT NULL,\n            title varchar(255) NOT NULL,\n    \t\tlanguage_code varchar(10) NOT NULL,\n    \t\twebsite_uri varchar(255),\n    \t\tregular_hours TEXT,\n        \tspecial_hours TEXT,\n        \tlabels TEXT,\n        \tmetadata TEXT,\n        \tstorefront_address TEXT,\n        \tservice_area TEXT,\n            imported_at datetime NOT NULL,\n\t\t\tin_latest_import tinyint(1) NOT NULL DEFAULT 1,\n            PRIMARY KEY  (id),\n    \t\tUNIQUE KEY `google_id` (`google_id`),\n    \t\tKEY `idx_group_id` (`group_id`)\n\t\t) {$charset_collate};";
        dbDelta( $sql );
    }

    /**
     * @return Container
     */
    public function get_container() {
        return $this->container;
    }

    /**
     * @return EventManager|bool
     */
    public function get_event_manager() {
        if ( !$this->is_loaded() ) {
            return false;
        }
        return $this->container['event_manager'];
    }

    public function get_autopost_factory() {
        if ( !$this->is_loaded() ) {
            return false;
        }
        return $this->container['factory.autopost_factory'];
    }

}
