<?php

namespace WpBookingEventFrontend\app\controllers;

use WpBookingEvent\Factory;
use WpBookingEvent\Filesystem\Support\StringController;
use WpBookingEvent\Mvc\Controller\ControllerFrontend;
use WpBookingEvent\Mvc\Model\Model;
use WpBookingEvent\Utilities\Utility;
use WpBookingEventFrontend\app\helpers\ConfigHelper as ConfigHelperAlias;
use WpBookingEventFrontend\app\helpers\EmailHelper;

defined('_WPBOOKINGEVENT_EXEC') or die;

/**
 * Class BlockController
 */
class BookingController extends ControllerFrontend
{
    public function book_service()
    {
        $appConfig = Factory::getAppConfig();
        $booking_style = $appConfig->get('booking_style', 'in_line');
        $client_secret_file = $appConfig->get('client_secret_file','');
        $is_enable_booking_add_gg_calendar_api = $appConfig->get('is_enable_booking_add_gg_calendar_api',0);
        $input = Factory::getInput();
        $data = $input->getData();
        $modelBooking = Model::getInstance('booking');
        $order = $modelBooking->book_service($data);
        if($is_enable_booking_add_gg_calendar_api) {
            if($client_secret_file !== '' && file_exists(WPBOOKINGEVENT_PATH_ROOT.DS.$client_secret_file)) {
                $modelBooking->add_google_calendar($order->id);
            } else {
                Utility::redirect(Factory::getRootUrl().'/wp-admin/admin.php?page=wb_config');
            }
        }
        Utility::redirect(Utility::getFrontendGoToLink("payment.paynow", array("order_id" => $order->id)));
    }
    public function book_event()
    {
        $appConfig = Factory::getAppConfig();
        $booking_style = $appConfig->get('booking_style', 'in_line');
        $client_secret_file = $appConfig->get('client_secret_file','');
        $is_enable_booking_add_gg_calendar_api = $appConfig->get('is_enable_booking_add_gg_calendar_api',0);
        $input = Factory::getInput();
        $data = $input->getData();
        $modelBooking = Model::getInstance('booking');
        $order = $modelBooking->book_event($data);
        if($is_enable_booking_add_gg_calendar_api) {
            if($client_secret_file !== '' && file_exists(WPBOOKINGEVENT_PATH_ROOT.DS.$client_secret_file)) {
                $modelBooking->add_google_calendar_event($order->id);
            }else{
                Utility::redirect(Factory::getRootUrl().'/wp-admin/admin.php?page=wb_config');
            }
        }
        Utility::redirect(Utility::getFrontendGoToLink("payment.paynow", array("order_id" => $order->id)));
    }
    public function book_now()
    {
        $appConfig = Factory::getAppConfig();
        $booking_style = $appConfig->get('booking_style', 'in_line');
        $client_secret_file = $appConfig->get('client_secret_file','');
        $is_enable_booking_add_gg_calendar_api = $appConfig->get('is_enable_booking_add_gg_calendar_api',0);
        $input = Factory::getInput();
        $data = $input->getData();
        $modelBooking = Model::getInstance('booking');
        $order = $modelBooking->book_now($data);

        if($is_enable_booking_add_gg_calendar_api) {
            if($client_secret_file !== '' && file_exists(WPBOOKINGEVENT_PATH_ROOT.DS.$client_secret_file)) {
                $modelBooking->add_google_calendar($order->id);
            }
        }

        Utility::redirect(Utility::getFrontendGoToLink("payment.paynow", array("order_id" => $order->id)));
    }
    function verify_google_calendar(){
        $client = new \Google_Client();
        $client->setScopes(\Google_Service_Calendar::CALENDAR);
        $appConfig = Factory::getAppConfig();
        $file_client_secret = $appConfig->get('client_secret_file','');
        if(isset($file_client_secret) && trim($file_client_secret) !== '' && file_exists(WPBOOKINGEVENT_PATH_ROOT.DS.$file_client_secret)) {
            $client->setAuthConfig(WPBOOKINGEVENT_PATH_ROOT.DS.$file_client_secret);
        } else {
            $client->setAuthConfig(WPBOOKINGEVENT_PATH_ROOT.DS.'client_secret_525629706282-i2g4hf4hqjm82m52cdkg06jsuva8oelt.apps.googleusercontent.com.json');
        }

        $modelBooking = Model::getInstance('booking');
        $input=Factory::getInput();
        $code=$input->getString('code','');

        if(trim($code)!=""){
            $client->authenticate($code);
            $_SESSION['access_token'] = $client->getAccessToken();
        }

        if(isset($_SESSION['current_oder_id']) && $_SESSION['current_oder_id']!=="")
        {

            $order_id=(int)$_SESSION['current_oder_id'];
            $modelBooking->add_google_calendar($order_id);

            Utility::redirect(Utility::getFrontendGoToLink("payment.paynow", array("order_id" => $order_id)));
            die;

        }else{
            throw new \Exception("error");
        }
    }
}
