/**
 * Plugin Setup (On Activation)
 * 
 * Does the initial setup,
 * stest default values for the plugin options.
 * 
 * @package Social Deals Engine
 * @since 1.0.0
 */
function wps_deals_install()
{
    global $wpdb, $user_ID, $wps_deals_query, $wps_deals_options;
    //register custom post type
    wps_deals_register_deal_post_type();
    wps_deals_register_sales_post_types();
    //add endpoints to query vars
    //Need to call before flush rewrite rules
    $wps_deals_query->add_endpoints();
    //IMP Call of Function
    //Need to call when custom post type is being used in plugin
    flush_rewrite_rules();
    //add capabilities to administrator roles
    wps_deals_add_capabilities();
    //get option for when plugin is activating first time
    $wps_deals_set_option = get_option('wps_deals_set_option');
    //get all option for the plugin
    $wps_deals_options = get_option('wps_deals_options');
    if (empty($wps_deals_options)) {
        //check plugin version option
        $deals_page = array('post_type' => 'page', 'post_status' => 'publish', 'post_title' => __('Social Deals', 'wpsdeals'), 'post_content' => '[wps_home_deals][/wps_home_deals][wps_deals_by_status active="true" ending_sooon="true" upcoming="true" ][/wps_deals_by_status]', 'post_author' => $user_ID, 'menu_order' => 0, 'comment_status' => 'closed');
        //create main page for plugin
        $deals_parent_page_id = wp_insert_post($deals_page);
        // checkout page creation
        $checkout_page = array('post_type' => 'page', 'post_status' => 'publish', 'post_title' => __('Social Deals Checkout', 'wpsdeals'), 'post_content' => '[wps_deals_checkout][/wps_deals_checkout]', 'post_author' => $user_ID, 'menu_order' => 0, 'comment_status' => 'closed');
        $checkout_page_id = wp_insert_post($checkout_page);
        //create my account page
        $deals_myaccount_page = array('post_type' => 'page', 'post_status' => 'publish', 'post_title' => __('My Account', 'wpsdeals'), 'post_content' => '[wps_deals_my_account][/wps_deals_my_account]', 'post_author' => $user_ID, 'menu_order' => 0, 'comment_status' => 'closed');
        //create my account page
        $myaccount_page_id = wp_insert_post($deals_myaccount_page);
        // this option contains all page ID(s) to just pass it to wps_deals_default_settings function
        update_option('wps_deals_set_pages', array('main_page' => $deals_parent_page_id, 'checkout_page' => $checkout_page_id, 'my_account_page' => $myaccount_page_id));
        // set default settings
        wps_deals_default_settings();
        //update plugin version to option
        update_option('wps_deals_set_option', '1.0');
        //update countires to database
        wps_deals_update_countries();
    }
    //check deals options empty or not
    $wps_deals_set_option = get_option('wps_deals_set_option');
    // Cron jobs
    wp_clear_scheduled_hook('wps_deals_scheduled_set_price_meta');
    wp_schedule_event(time(), 'daily', 'wps_deals_scheduled_set_price_meta');
    if ($wps_deals_set_option == '1.0') {
        //check set option for plugin is set 1.0
        $udpopt = false;
        if (!isset($wps_deals_options['default_payment_gateway'])) {
            //check default gateway is set or not
            $defaultgateway = array('default_payment_gateway' => 'paypal');
            $wps_deals_options = array_merge($wps_deals_options, $defaultgateway);
            $udpopt = true;
        }
        if (!isset($wps_deals_options['caching'])) {
            //check caching is set or not
            $caching = array('caching' => '');
            $wps_deals_options = array_merge($wps_deals_options, $caching);
            $udpopt = true;
        }
        if (!isset($wps_deals_options['cheque_customer_msg'])) {
            //check Customer Message is set or not
            $cheque_customer_msg = array('cheque_customer_msg' => __('Please send your cheque to Store Name, Store Street, Store Town, Store State / County, Store Postcode.', 'wpsdeals'));
            $wps_deals_options = array_merge($wps_deals_options, $cheque_customer_msg);
            $udpopt = true;
        }
        if (!isset($wps_deals_options['login_heading'])) {
            //check Social Login Title is set or not
            $login_heading = array('login_heading' => __('Login with Social Media', 'wpsdeals'));
            $wps_deals_options = array_merge($wps_deals_options, $login_heading);
            $udpopt = true;
        }
        if (!isset($wps_deals_options['login_redirect_url'])) {
            //check Redirect URL is set or not
            $login_redirect_url = array('login_redirect_url' => '');
            $wps_deals_options = array_merge($wps_deals_options, $login_redirect_url);
            $udpopt = true;
        }
        if (!isset($wps_deals_options['email_template'])) {
            //check Email Template is set or not
            $email_template = array('email_template' => '');
            $wps_deals_options = array_merge($wps_deals_options, $email_template);
            $udpopt = true;
        }
        if ($udpopt == true) {
            // if any of the settings need to be updated
            update_option('wps_deals_options', $wps_deals_options);
        }
        //update plugin version to option
        update_option('wps_deals_set_option', '1.0.1');
    }
    //check plugin set option value is 1.0
    $wps_deals_set_option = get_option('wps_deals_set_option');
    if ($wps_deals_set_option == '1.0.1') {
        $udpopt = false;
        //check enable billin is set in options or not
        if (!isset($wps_deals_options['enable_billing'])) {
            $billing = array('enable_billing' => '');
            $wps_deals_options = array_merge($wps_deals_options, $billing);
            $udpopt = true;
        }
        //check reset password email subject is set in options or not
        if (!isset($wps_deals_options['reset_password_email_subject'])) {
            $email_subject = array('reset_password_email_subject' => __('Reset Password', 'wpsdeals'));
            $wps_deals_options = array_merge($wps_deals_options, $email_subject);
            $udpopt = true;
        }
        //check reset password email body is set in options or not
        if (!isset($wps_deals_options['reset_password_email'])) {
            $email_sbody = array('reset_password_email' => sprintf(__('Someone requested that the password be reset for the following account:' . "\n\n" . 'Username: {%s}' . "\n\n" . 'If this was a mistake, just ignore this email and nothing will happen.' . "\n\n" . 'To reset your password, visit the following address:' . "\n\n" . '{%s}', 'wpsdeals'), 'user_name', 'reset_link'));
            $wps_deals_options = array_merge($wps_deals_options, $email_sbody);
            $udpopt = true;
        }
        if ($udpopt == true) {
            // if any of the settings need to be updated
            update_option('wps_deals_options', $wps_deals_options);
        }
        //update plugin version to option
        update_option('wps_deals_set_option', '1.0.2');
    }
    //check plugin set option value is 1.0.1
    $wps_deals_set_option = get_option('wps_deals_set_option');
    if ($wps_deals_set_option == '1.0.2') {
        $udpopt = false;
        //check reset password email body is set in options or not
        if (!isset($wps_deals_options['cheque_title'])) {
            $cheque_title = array('cheque_title' => __('Cheque Payment', 'wpsdeals'));
            $wps_deals_options = array_merge($wps_deals_options, $cheque_title);
            $udpopt = true;
        }
        if ($udpopt == true) {
            // if any of the settings need to be updated
            update_option('wps_deals_options', $wps_deals_options);
        }
        //update plugin version to option
        update_option('wps_deals_set_option', '1.0.3');
    }
    //check plugin set option value is 1.0.2
    $wps_deals_set_option = get_option('wps_deals_set_option');
    if ($wps_deals_set_option == '1.0.3') {
        $udpopt = false;
        //check force ssl checkout settings is set or not
        if (!isset($wps_deals_options['force_ssl_checkout'])) {
            $force_ssl = array('force_ssl_checkout' => '');
            $wps_deals_options = array_merge($wps_deals_options, $force_ssl);
            $udpopt = true;
        }
        //check unforce ssl checkout settings is set or not
        if (!isset($wps_deals_options['unforce_ssl_checkout'])) {
            $unforce_ssl = array('unforce_ssl_checkout' => '');
            $wps_deals_options = array_merge($wps_deals_options, $unforce_ssl);
            $udpopt = true;
        }
        if ($udpopt == true) {
            // if any of the settings need to be updated
            update_option('wps_deals_options', $wps_deals_options);
        }
        //update plugin version to option
        update_option('wps_deals_set_option', '1.0.4');
    }
    //check plugin set option value is 1.0.3
    $wps_deals_set_option = get_option('wps_deals_set_option');
    if ($wps_deals_set_option == '1.0.4') {
        $udpopt = false;
        //check default deals orderby settings is set or not
        if (!isset($wps_deals_options['default_deals_orderby'])) {
            $default_deals_orderby = array('default_deals_orderby' => 'date-desc');
            $wps_deals_options = array_merge($wps_deals_options, $default_deals_orderby);
            $udpopt = true;
        }
        //check enable deals orderby settings
        if (!isset($wps_deals_options['enable_deals_orderby'])) {
            $enable_deals_orderby = array('enable_deals_orderby' => '');
            $wps_deals_options = array_merge($wps_deals_options, $enable_deals_orderby);
            $udpopt = true;
        }
        if ($udpopt == true) {
            // if any of the settings need to be updated
            update_option('wps_deals_options', $wps_deals_options);
        }
        //update plugin version to option
        update_option('wps_deals_set_option', '1.0.5');
    }
    //check plugin set option value is 1.0.4
    $wps_deals_set_option = get_option('wps_deals_set_option');
    if ($wps_deals_set_option == '1.0.5') {
        //update plugin version to option
        update_option('wps_deals_set_option', '1.0.6');
    }
    //check plugin set option value is 1.0.5
    $wps_deals_set_option = get_option('wps_deals_set_option');
    if ($wps_deals_set_option == '1.0.6') {
        $udpopt = false;
        // check chome deals
        if (!isset($wps_deals_options['deals_home'])) {
            $deals_home = array('deals_home' => 'deals-col-6');
            $wps_deals_options = array_merge($wps_deals_options, $deals_home);
            $udpopt = true;
        }
        // check single deal size
        if (!isset($wps_deals_options['deals_size_single'])) {
            $deals_size_single = array('deals_size_single' => 'medium');
            $wps_deals_options = array_merge($wps_deals_options, $deals_size_single);
            $udpopt = true;
        }
        // check archive deals size
        if (!isset($wps_deals_options['deals_size_archive'])) {
            $deals_size_archive = array('deals_size_archive' => 'medium');
            $wps_deals_options = array_merge($wps_deals_options, $deals_size_archive);
            $udpopt = true;
        }
        // check button color
        if (!isset($wps_deals_options['deals_btn_color'])) {
            $deals_btn_color = array('deals_btn_color' => 'blue');
            $wps_deals_options = array_merge($wps_deals_options, $deals_btn_color);
            $udpopt = true;
        }
        // check home columns
        if (!isset($wps_deals_options['deals_columns'])) {
            $deals_columns = array('deals_columns' => 'deals-col-6');
            $wps_deals_options = array_merge($wps_deals_options, $deals_columns);
            $udpopt = true;
        }
        // check archive columns
        if (!isset($wps_deals_options['deals_columns_archive'])) {
            $deals_columns_archive = array('deals_columns_archive' => 'deals-col-6');
            $wps_deals_options = array_merge($wps_deals_options, $deals_columns_archive);
            $udpopt = true;
        }
        if ($udpopt == true) {
            // if any of the settings need to be updated
            update_option('wps_deals_options', $wps_deals_options);
        }
        // update plugin version to option
        update_option('wps_deals_set_option', '1.2.0');
    }
    //check plugin set option value is 1.0.6
    //Change Log file Dir and create directory on activation
    wps_deals_create_files();
    $wps_deals_set_option = get_option('wps_deals_set_option');
    if ($wps_deals_set_option == '1.2.0') {
        $udpopt = false;
        // create shop page
        if (empty($wps_deals_options['shop_page'])) {
            // home shop creation
            $deals_shop_page = array('post_type' => 'page', 'post_status' => 'publish', 'post_title' => __('Deals Shop', 'wpsdeals'), 'post_author' => $user_ID, 'menu_order' => 0, 'comment_status' => 'closed');
            // create shop page
            $deals_shop_page_id = wp_insert_post($deals_shop_page);
            // get set pages option data
            $wps_deals_set_pages = get_option('wps_deals_set_pages');
            // store shop page to already created page
            $wps_deals_set_pages['shop_page'] = $deals_shop_page_id;
            // update set pages option data
            update_option('wps_deals_set_pages', $wps_deals_set_pages);
            $shoppage = array('shop_page' => $deals_shop_page_id);
            // set page to options
            $wps_deals_options = array_merge($wps_deals_options, $shoppage);
            $udpopt = true;
        }
        //check enable breadcrumb is set in options or not
        if (!isset($wps_deals_options['enable_breadcrumb'])) {
            $breadcrumb = array('enable_breadcrumb' => '1');
            $wps_deals_options = array_merge($wps_deals_options, $breadcrumb);
            $udpopt = true;
        }
        if ($udpopt == true) {
            // if any of the settings need to be updated
            update_option('wps_deals_options', $wps_deals_options);
        }
        // update plugin version to option
        update_option('wps_deals_set_option', '1.2.1');
    }
    //check plugin set option value is 1.2.0
    $wps_deals_set_option = get_option('wps_deals_set_option');
    if ($wps_deals_set_option == '1.2.1') {
        $udpopt = false;
        // check item quantities is set in options or not
        if (!isset($wps_deals_options['item_quantities'])) {
            $item_quantities = array('item_quantities' => '');
            $wps_deals_options = array_merge($wps_deals_options, $item_quantities);
            $udpopt = true;
        }
        if ($udpopt == true) {
            // if any of the settings need to be updated
            update_option('wps_deals_options', $wps_deals_options);
        }
        // update plugin version to option
        update_option('wps_deals_set_option', '1.2.2');
    }
    //check plugin set option value is 1.2.1
    $wps_deals_set_option = get_option('wps_deals_set_option');
    if ($wps_deals_set_option == '1.2.2') {
        //get values for created pages
        $pages = get_option('wps_deals_set_pages');
        if (!empty($pages['checkout_page'])) {
            $chekout_page = array('ID' => $pages['checkout_page'], 'post_parent' => 0, 'menu_order' => 0);
            //Update the checkout page
            wp_update_post($chekout_page);
        }
        // update plugin version to option
        update_option('wps_deals_set_option', '1.2.3');
    }
    //check plugin set option value is 1.2.2
    $wps_deals_set_option = get_option('wps_deals_set_option');
    if ($wps_deals_set_option == '1.2.3') {
        $udpopt = false;
        if (!isset($wps_deals_options['purchase_limit_label'])) {
            $wps_deals_options['purchase_limit_label'] = __('Sold Out', 'wpsdeals');
            $udpopt = true;
        }
        if ($udpopt == true) {
            // if any of the settings need to be updated
            update_option('wps_deals_options', $wps_deals_options);
        }
        // update plugin version to option
        update_option('wps_deals_set_option', '1.2.4');
    }
    //check plugin set option value is 1.2.3
    $wps_deals_set_option = get_option('wps_deals_set_option');
    if ($wps_deals_set_option == '1.2.4') {
        // this option contains end-points permalinks options
        $wps_deals_options['deals_thank_you_page_endpoint'] = 'social-deals-thank-you-page';
        $wps_deals_options['deals_cancel_page_endpoint'] = 'social-deals-cancel-page';
        $wps_deals_options['edit_account_endpoint'] = 'edit-account';
        $wps_deals_options['edit_address_endpoint'] = 'edit-address';
        $wps_deals_options['lost_password_endpoint'] = 'lost-password';
        $wps_deals_options['view_orders_endpoint'] = 'view-orders';
        $wps_deals_options['create_an_account_endpoint'] = 'create-an-account';
        // Update end-points permalinks options
        update_option('wps_deals_options', $wps_deals_options);
        // update plugin version to option
        update_option('wps_deals_set_option', '1.2.5');
    }
    //check plugin set option value is 1.2.4
    $wps_deals_set_option = get_option('wps_deals_set_option');
    if ($wps_deals_set_option == '1.2.4') {
        if (!isset($wps_deals_options['enable_lightbox'])) {
            $lightbox = array('enable_lightbox' => '1');
            $wps_deals_options = array_merge($wps_deals_options, $lightbox);
            $udpopt = true;
        }
        if ($udpopt == true) {
            // if any of the settings need to be updated
            update_option('wps_deals_options', $wps_deals_options);
        }
        // update plugin version to option
        update_option('wps_deals_set_option', '1.2.5');
    }
    $wps_deals_set_option = get_option('wps_deals_set_option');
    if ($wps_deals_set_option == '1.2.5') {
        // future code here
    }
    //check plugin set option value is 1.2.5
    //Action for theme supports
    do_action('wpsdeals_updated');
}
$message = $wps_deals_message;
?>
	<!-- wpsocial logo -->
	<img src="<?php 
echo WPS_DEALS_URL . 'includes/images/wps-logo.png';
?>
" class="wpsocial-logo" alt="WPSocial.com Logo" />
	<!-- plugin name -->
	<h2 class="wps-deals-settings-title"><?php 
_e('Social Deals Engine - Settings', 'wpsdeals');
?>
</h2><br />
	<!-- settings reset -->
	<?php 
if (isset($_POST['wps_deals_reset_settings']) && $_POST['wps_deals_reset_settings'] == __('Reset All Settings', 'wpsdeals')) {
    wps_deals_default_settings();
    // set default settings
    //reseting facebook fan page posting setting session
    echo '<div id="message" class="updated fade"><p><strong>' . __('All Settings Reset Successfully.', 'wpsdeals') . '</strong></p></div>';
    //do selected tab for general
    $message->add_session('wps-deals-selected-tab', strtolower('General'));
    // Need to rewrite endpoints when settings changed
    wps_deals_rewrite_endpoints();
} else {
    if (isset($_GET['settings-updated']) && !empty($_GET['settings-updated'])) {
        //check settings updated or not
        //reseting facebook fan page posting setting session
        echo '<div id="message" class="updated fade"><p><strong>' . __('Changes Saved.', 'wpsdeals') . '</strong></p></div>';
        // Need to rewrite endpoints when settings changed
        wps_deals_rewrite_endpoints();
    }