<?php
    
use Tygh\Http;
use Tygh\Registry;

if (!defined('BOOTSTRAP')) { die('Access denied'); }
    
function fn_incor_get_shipping_methods() {
    return db_get_hash_single_array("SELECT a.shipping_id, b.shipping FROM ?:shippings as a LEFT JOIN ?:shipping_descriptions as b ON a.shipping_id=b.shipping_id AND b.lang_code = '" . CART_LANGUAGE . "' ORDER BY a.position", array('shipping_id', 'shipping'));
}

function fn_incor_get_order_statuses() {
    return db_get_hash_single_array("SELECT a.status, b.description FROM ?:statuses AS a LEFT JOIN ?:status_descriptions AS b ON a.status_id = b.status_id AND b.lang_code = ?s WHERE a.type = ?s", array('status', 'description'), CART_LANGUAGE,'O');
}

function fn_incor_get_form_url_info() {
    return __('incor_form_info_text');
}

function fn_incor_create_shipment(&$shipment_data, &$order_info, &$group_key, &$all_products) {
    $notify_user = fn_get_notification_rules($_REQUEST);
    $notify_user = $notify_user['C'];
    if ($notify_user == true) {
        if (empty($order_info['b_phone']) && empty($order_info['s_phone']) && empty($order_info['phone'])) {
            return;
        }
        
        $path = Registry::get('config.dir.addons') . 'incor/';
        
        include ($path . 'vendor/autoload.php');
        
        $phone_area = Registry::get('addons.incor.customer_phone_field');
        
        $phone_field = $phone_area . '_phone';
        
        $phone = $order_info[$phone_field];
        
        $country_field = $phone_area . '_country';
        
        $country = $order_info[$country_field];
        
        if (empty($phone)) {
            // If empty this field, than try to use default phone field
            if (!empty($order_info['phone'])) {
                $phone = $order_info['phone'];
            }
        }
        
        if (empty($country)) {
            if (!empty($order_info['country'])) {
                $country = $order_info['country'];
            } else {
                $country = Registry::get('settings.General.default_country');
            }
        }
        
        // Convert phone into E164 format
        
        $phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
        
        $phone_proto = $phoneUtil->parse($phone, $country);
        
        $phone = $phoneUtil->format($phone_proto, \libphonenumber\PhoneNumberFormat::E164);
        
        $shipping = db_get_field('SELECT shipping FROM ?:shipping_descriptions WHERE shipping_id = ?i AND lang_code = ?s', $shipment_data['shipping_id'], $order_info['lang_code']);
        
        Tygh::$app['view']->assign('tracking_number', $shipment_data['tracking_number']);
        Tygh::$app['view']->assign('order_id', $order_info['order_id']);
        Tygh::$app['view']->assign('shipping_method', $shipping);
        
        $body = Tygh::$app['view']->fetch('design/backend/templates/addons/incor/views/sms/components/new_shipment_sms.tpl');
        
        $body = fn_incor_strip_tags($body);
        
        fn_incor_send_sms($phone, $body);
    }
}

function fn_incor_change_order_status(&$status_to, &$status_from, &$order_info, &$force_notification, &$order_statuses, &$place_order) {

    if (Registry::get('addons.incor.admin_sms_order_updated') == 'Y' || Registry::get('addons.incor.customer_sms_order_updated') == 'Y') {
        
        $order_id = $order_info['order_id'];
        
        Tygh::$app['view']->assign('order_id', $order_id);
        Tygh::$app['view']->assign('total', $order_info['total']);
        Tygh::$app['view']->assign('order_email', $order_info['email']);
        Tygh::$app['view']->assign('order_payment_info', $order_info['payment_method']['payment']);
        Tygh::$app['view']->assign('order_status_name', $order_statuses[$status_to]['description']);
        
        $body = '';
        
        if (Registry::get('addons.incor.admin_sms_order_updated') == 'Y' && $place_order == false) {
            $result = fn_incor_check_order_conditions('admin', $status_to, $order_info, $order_statuses);
            if ($result == true) {
                $body = Tygh::$app['view']->fetch('design/backend/templates/addons/incor/views/sms/components/admin_order_updated_sms.tpl');
                $body = fn_incor_strip_tags($body);
                $phone = Registry::get('addons.incor.admin_phone_number');
                $phone = explode(',', $phone);
                foreach($phone as $k => $v) {
                    fn_incor_send_sms($v, $body);
                }
            }
        }
        
        if (Registry::get('addons.incor.customer_sms_order_updated') == 'Y') {
            
            if (empty($order_info['b_phone']) && empty($order_info['s_phone']) && empty($order_info['phone'])) {
                return;
            }
            
            $path = Registry::get('config.dir.addons') . 'incor/';
            
            include ($path . 'vendor/autoload.php');
            
            $phone_area = Registry::get('addons.incor.customer_phone_field');
            
            $phone_field = $phone_area . '_phone';
            
            $phone = $order_info[$phone_field];
            
            $country_field = $phone_area . '_country';
            
            $country = $order_info[$country_field];
            
            if (empty($phone)) {
                // If empty this field, than try to use default phone field
                if (!empty($order_info['phone'])) {
                    $phone = $order_info['phone'];
                }
            }
            
            if (empty($country)) {
                if (!empty($order_info['country'])) {
                    $country = $order_info['country'];
                } else {
                    $country = Registry::get('settings.General.default_country');
                }
            }
            
            // Convert phone into E164 format
            
            $phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
            
            $phone_proto = $phoneUtil->parse($phone, $country);
            
            $phone = $phoneUtil->format($phone_proto, \libphonenumber\PhoneNumberFormat::E164);

            $result = fn_incor_check_order_conditions('customer', $status_to, $order_info, $order_statuses);
            
            if ($result == true) {
                if ($order_statuses[$status_to]['sms_body']) {
                    
                    $replacements = array(
			'%ORDER_ID%' => $order_id,
                        '%AMOUNT%' => $order_info['total'],
                        '%NAME%' => $order_info['firstname'],
                        '%LAST_NAME%' => $order_info['lastname'],
                        '%USER_EMAIL%' => $order_info['email'],
                        '%COUNTRY%' => fn_get_country_name($order_info[$phone_area . '_country']),
                        '%ADDRESS%' => $order_info[$phone_area . '_address'],
                        '%CITY%' => $order_info[$phone_area . '_city'],
                        '%STATE%' => $order_info[$phone_area . '_state'],
                    );
                    
                    $body = $order_statuses[$status_to]['sms_body'];
                    $body = str_replace(array_keys($replacements), $replacements, $body);
                    
                    $body = fn_incor_strip_tags($body);
                    
                    fn_incor_send_sms($phone, $body);
                }
            }
        }
    }
}

function fn_incor_place_order(&$order_id, &$action, &$fake1, &$cart) {
    if ($action !== 'save' && Registry::get('addons.incor.admin_sms_new_order_placed') == 'Y') {
        Tygh::$app['view']->assign('order_id', $order_id);
        Tygh::$app['view']->assign('total', $cart['total']);
        
        $send_info = Registry::get('addons.incor.admin_sms_send_payment_info');
        $send_email = Registry::get('addons.incor.admin_sms_send_customer_email');
        $send_min_amount = Registry::get('addons.incor.admin_sms_send_min_amout');
        $shippings = Registry::get('addons.incor.admin_sms_send_shipping');
        
        if (!is_array($shippings)) {
            $shippings = array ();
        }
        
        Tygh::$app['view']->assign('send_info', $send_info == 'Y' ? true : false);
        Tygh::$app['view']->assign('send_email', $send_email == 'Y' ? true : false);
        Tygh::$app['view']->assign('send_min_amount', $send_min_amount == 'Y' ? true : false);
        
        $order = fn_get_order_info($order_id);
        
        Tygh::$app['view']->assign('order_email', $order['email']);
        Tygh::$app['view']->assign('order_payment_info', $order['payment_method']['payment']);
        
        if (count($shippings) && !isset($shippings['N'])) {
            $in_shipping = false;

            if (!empty($order['shipping'])) {
                foreach ($order['shipping'] as $data) {
                    $id = $data['shipping_id'];
                    if ($shippings[$id] == 'Y') {
                        $in_shipping = true;
                        break;
                    }
                }
            }
        } else {
            $in_shipping = true;
        }

        if ($in_shipping && $order['subtotal'] > doubleval($send_min_amount)) {
            $body = Tygh::$app['view']->fetch('design/backend/templates/addons/incor/views/sms/components/admin_order_place_sms.tpl');
            $body = fn_incor_strip_tags($body);
            $phone = Registry::get('addons.incor.admin_phone_number');
            $phone = explode(',', $phone);
            foreach($phone as $k => $v) {
                fn_incor_send_sms($v, $body);
            }
        }        
    }
}

function fn_incor_update_profile(&$action, &$user_data) {
    if ($action == 'add' && AREA == 'C' && Registry::get('addons.incor.admin_sms_new_cusomer_registered') == 'Y') {
        Tygh::$app['view']->assign('customer', $user_data['firstname'] . (empty($user_data['lastname']) ? '' : $user_data['lastname']));
        $body = Tygh::$app['view']->fetch('design/backend/templates/addons/incor/views/sms/components/new_profile_sms.tpl');
        $body = fn_incor_strip_tags($body);
        $phone = Registry::get('addons.incor.admin_phone_number');
        $phone = explode(',', $phone);
        foreach($phone as $k => $v) {
            fn_incor_send_sms($v, $body);
        }
    }    
}

function fn_incor_update_product_amount(&$new_amount, &$product_id) {
    if ($new_amount <= Registry::get('settings.General.low_stock_threshold') && Registry::get('addons.incor.admin_sms_product_negative_amount') == 'Y') {
        $lang_code = Registry::get('settings.Appearance.backend_default_language');

        Tygh::$app['view']->assign('product_id', $product_id);
        Tygh::$app['view']->assign('product', db_get_field("SELECT product FROM ?:product_descriptions WHERE product_id = ?i AND lang_code = ?s", $product_id, $lang_code));
        $body = Tygh::$app['view']->fetch('design/backend/templates/addons/incor/views/sms/components/low_stock_sms.tpl');
        $body = fn_incor_strip_tags($body);
        $phone = Registry::get('addons.incor.admin_phone_number');
        $phone = explode(',', $phone);
        foreach($phone as $k => $v) {
            fn_incor_send_sms($v, $body);
        }
    }    
}

function fn_incor_check_order_conditions($for = 'admin', $status_to, $order, $order_statuses) {
    $send_min_amount = Registry::get('addons.incor.' . $for . '_sms_send_min_amount');
    $shippings = Registry::get('addons.incor.' . $for . '_sms_send_shipping');
    $statuses = Registry::get('addons.incor.' . $for . '_sms_send_order_statuses');
    if ($for == 'admin') {
        $send_email = Registry::get('addons.incor.admin_sms_send_customer_email');
    }
    
    if (!is_array($statuses)) {
        $statuses = array();
    }

    if (!is_array($shippings)) {
        $shippings = array ();
    }

    if (count($shippings) && !isset($shippings['N'])) {
        $in_shipping = false;

        if (!empty($order['shipping'])) {
            foreach ($order['shipping'] as $data) {
                $id = $data['shipping_id'];
                if ($shippings[$id] == 'Y') {
                    $in_shipping = true;
                    break;
                }
            }
        }
    } else {
        $in_shipping = true;
    }

    if (count($statuses) && !isset($statuses['N'])) {
        $in_status = false;
        if ($statuses[$status_to] == 'Y') {
            $in_status = true;
        }
        // check if status N is a status
        if (isset($statuses['N']) && empty($statuses['N'])) {
            $in_status = true;
        }
    } else {
        $in_status = true;
    }
    
    if ($in_status == true && $in_shipping == true && $order['subtotal'] > doubleval($send_min_amount)) {
        return true;
    }
    return false;
}

function fn_incor_send_sms($phone, $body) {
    
    $phone = trim($phone);
    
    if (!$phone || !$body) {
        return false;
    }
    
    $first_symbol = substr($phone, 0, 1);
    
    if ($first_symbol == '+') {
        $phone = substr($phone, 1);
    }
    
    $fail_responces = array(
        'У нас закончились SMS. Для разрешения проблемы свяжитесь с менеджером.',
        'Закончились SMS.',
        'Аккаунт заблокирован.',
        'Укажите номер телефона.',
        'Номер телефона присутствует в стоп-листе.',
        'Данное направление закрыто для вас.',
        'Данное направление закрыто.',
        'Недостаточно средств для отправки SMS. SMS будет отправлена как только вы пополните счет по данному направлению.',
        'Текст SMS отклонен модератором.',
        'Нет отправителя.',
        'Отправитель не должен превышать 15 символов для цифровых номеров и 11 символов для буквенно-числовых.',
        'Номер телефона должен быть меньше 15 символов.',
        'Нет текста сообщения.',
        'Нет ссылки.',
        'Такого отправителя Нет.',
        'Отправитель не прошел модерацию.',
        'error: Попытка отправки более одного одинакового запроса в течение минуты'
    );
    
    $data = array(
        'user' => Registry::get('addons.incor.incor_login'),
        'pwd' => Registry::get('addons.incor.incor_password'),
        'sadr' => Registry::get('addons.incor.incor_sender'),
        'dadr' => $phone,
        'text' => $body
    );
    
    $result = Http::get('https://' . Registry::get('addons.incor.incor_host') . '/sendsms.php', $data);
    
    if (AREA == 'A' && in_array($result, $fail_responces)) {
        fn_set_notification('W', __('warning'), $result);
    }
}

function fn_incor_get_auth() {
    return array(
        'login' => Registry::get('addons.incor.incor_login'),
        'psw' => Registry::get('addons.incor.incor_password')
    );
}

/**
 * Strip html tags from the data
 *
 * @param mixed $var variable to strip tags from
 * @return mixed filtered variable
 */
function fn_incor_strip_tags(&$var)
{

    if (!is_array($var)) {
        return (strip_tags($var));
    } else {
        $stripped = array();
        foreach ($var as $k => $v) {
            $sk = strip_tags($k);
            if (!is_array($v)) {
                $sv = strip_tags($v);
            } else {
                $sv = fn_strip_tags($v);
            }
            $stripped[$sk] = $sv;
        }

        return ($stripped);
    }
}