<?php
namespace Bookly\Backend\Modules\CloudVoice;

use Bookly\Lib;

class Ajax extends Lib\Base\Ajax
{
    /**
     * Save settings
     *
     * @return void
     */
    public static function cloudVoiceSaveSettings()
    {
        $cloud = Lib\Cloud\API::getInstance();
        $cloud->getProduct( Lib\Cloud\Account::PRODUCT_VOICE )->setSettings( self::parameter( 'language' ) )
            ? wp_send_json_success()
            : wp_send_json_error( array( 'message' => current( $cloud->getErrors() ) ) );
    }

    /**
     * Make a test call
     *
     * @return void
     */
    public static function makeTestCall()
    {
        $cloud = Lib\Cloud\API::getInstance();
        $phone_number = self::parameter( 'phone_number' );
        $cloud->getProduct( Lib\Cloud\Account::PRODUCT_VOICE )->call( $phone_number, 'Hello, this is a test call from Bookly', 'Hello, this is a test call from Bookly' )
            ? wp_send_json_success( array( 'message' => sprintf( __( 'Calling %s', 'bookly' ), $phone_number ) . ' …' ) )
            : wp_send_json_error( array( 'message' => current( $cloud->getErrors() ) ?: __( 'Failed', 'bookly' ) ) );
    }

    /**
     * Get calls list
     *
     * @return void
     */
    public static function getCallsList()
    {
        $filter = self::parameter( 'filter' );
        $range  = $filter['range'];

        if ( $range === 'any' ) {
            $start = Lib\Utils\DateTime::applyTimeZoneOffset( date( 'Y-m-d', strtotime( '-100 year' ) ), 0 );
            $end   = Lib\Utils\DateTime::applyTimeZoneOffset( date( 'Y-m-d', strtotime( '+1 day' ) ), 0 );
        } else {
            $dates = explode( ' - ', $range, 2 );
            $start = Lib\Utils\DateTime::applyTimeZoneOffset( $dates[0], 0 );
            $end   = Lib\Utils\DateTime::applyTimeZoneOffset( date( 'Y-m-d', strtotime( '+1 day', strtotime( $dates[1] ) ) ), 0 );
        }

        $result = Lib\Cloud\API::getInstance()->getProduct( Lib\Cloud\Account::PRODUCT_VOICE )->getCallsList( $start, $end );
        $list   = isset( $result['list'] ) ? $result['list'] : array();

        Lib\Utils\Tables::updateSettings( Lib\Utils\Tables::VOICE_DETAILS, null, null, $filter );

        wp_send_json( array(
            'draw'            => (int) self::parameter( 'draw' ),
            'recordsTotal'    => count( $list ),
            'recordsFiltered' => count( $list ),
            'data'            => $list,
        ) );
    }

    /**
     * Get voice price-list.
     */
    public static function getVoicePriceList()
    {
        $result = Lib\Cloud\API::getInstance()->getProduct( Lib\Cloud\Account::PRODUCT_VOICE )->getPriceList();
        $list   = isset( $result['list'] ) ? $result['list'] : array();

        wp_send_json( array(
            'draw'            => (int) self::parameter( 'draw' ),
            'recordsTotal'    => count( $list ),
            'recordsFiltered' => count( $list ),
            'data'            => $list,
        ) );
    }
}