<?php
/**
 * @copyright 2021 Double Bastion LLC <www.doublebastion.com>
 *
 * @author Double Bastion LLC
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

declare(strict_types=1);

namespace OCA\SIPTripPhone\Service;

use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use OCP\Security\ICrypto;


class SphoneService {

    private $connection;
    private $crypto;

    public function __construct(IDBConnection $connection, ICrypto $crypto) {
                $this->connection = $connection;
                $this->crypto = $crypto;
    }

    /**
     * @NoAdminRequired
     *
     */
    public function getsettings($userId) {

        $sql0 = "SELECT * FROM `*PREFIX*sip_trip_phone` WHERE `user_id` = '$userId'";

        $res0 = $this->connection->prepare($sql0);
        $res0->execute();

        $settingsdb = $res0->fetch();

        if ($settingsdb['sipuserpassword'] != '' && $settingsdb['sipuserpassword'] != null && $settingsdb['sipuserpassword'] != 'undefined') {
            $settingsdb['sipuserpassword'] = "%20%20%20%20%20%20%20";
        } else { $settingsdb['sipuserpassword'] = ''; }

        $res0->closeCursor();

        return $settingsdb;
    }

    /**
     * @NoAdminRequired
     *
     */
    public function getsippass($userId) {

        $sql0 = "SELECT `id`, `user_id`, `sipuserpassword` FROM `*PREFIX*sip_trip_phone` WHERE `user_id` = '$userId'";

        $res0 = $this->connection->prepare($sql0);
        $res0->execute();

        $settingsdb = $res0->fetch();
        if ($settingsdb['sipuserpassword'] != '' && $settingsdb['sipuserpassword'] != null && $settingsdb['sipuserpassword'] != 'undefined') {
            $sipuserpassworddecr = $this->crypto->decrypt($settingsdb['sipuserpassword'], $password = '');
            $sippassword = $sipuserpassworddecr;
        }
        $res0->closeCursor();

        return $sippassword;
    }

    /**
     * @NoAdminRequired
     *
     */
    public function updatesettings($userId, $pdisplayname, $sipusername, $sipuserpassword, $stphwssurl, $siprealm, $stunserver) {

        $sql1 = "SELECT * FROM `*PREFIX*sip_trip_phone` WHERE `user_id` = '$userId'";
        $res1 = $this->connection->prepare($sql1);
        $res1->execute();

        $row = $res1->fetch();
        $res1->closeCursor();

        if ($row['user_id'] == '' || $row['user_id'] == 'undefined' || $row['user_id'] == null) {

            if ($sipuserpassword != '') {
                $sipuserpasswordenc = $this->crypto->encrypt($sipuserpassword, $password = '');                
            } else { $sipuserpasswordenc = ''; }

            $sql2 = "INSERT INTO `*PREFIX*sip_trip_phone` (`user_id`, `pdisplayname`, `sipusername`, `sipuserpassword`, `stphwssurl`, `siprealm`, `stunserver`)
                     VALUES ('$userId', '$pdisplayname', '$sipusername', '$sipuserpasswordenc', '$stphwssurl', '$siprealm', '$stunserver')";
            $res2 = $this->connection->prepare($sql2);
            $res2->execute();

        } else {

            if ($sipuserpassword != '' && $sipuserpassword != "%20%20%20%20%20%20%20") {
                $sipuserpasswordenc = $this->crypto->encrypt($sipuserpassword, $password = '');                
            } elseif ($sipuserpassword == "%20%20%20%20%20%20%20") {
                $sipuserpasswordenc = $row['sipuserpassword'];
            } elseif ($sipuserpassword == '') {
                $sipuserpasswordenc = '';
            }

            $sql3 = "UPDATE `*PREFIX*sip_trip_phone` SET `pdisplayname` = '$pdisplayname', `sipusername` = '$sipusername', `sipuserpassword` = '$sipuserpasswordenc',
                    `stphwssurl` = '$stphwssurl', `siprealm` = '$siprealm', `stunserver` = '$stunserver'  WHERE `user_id` = '$userId'";
            $res3 = $this->connection->prepare($sql3);
            $res3->execute();
        }
    }
}