<?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) {

        $sql = $this->connection->prepare('
                    SELECT `id`, `user_id`, `pdisplayname`, `sipusername`, `sipuserpassword`, `stphwssurl`, `siprealm`, `stunserver`
		    FROM  `*PREFIX*sip_trip_phone`
		    WHERE `user_id` = ?');
        $result = $sql->execute([$userId]);
        $settingsdb = $result->fetch();
        $result->closeCursor();

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

            return $settingsdb;
        }
    }

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

        $sqlps = $this->connection->prepare('
                    SELECT `id`, `user_id`, `sipuserpassword`
		    FROM  `*PREFIX*sip_trip_phone`
		    WHERE `user_id` = ?');
        $resultps = $sqlps->execute([$userId]);
        $settingsdb = $resultps->fetch();
        $resultps->closeCursor();

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

            return $sippassword;
        }
    }

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

        $sqlup = $this->connection->prepare('
                    SELECT `id`, `user_id`, `pdisplayname`, `sipusername`, `sipuserpassword`, `stphwssurl`, `siprealm`, `stunserver`
		    FROM  `*PREFIX*sip_trip_phone`
		    WHERE `user_id` = ?');
        $resultup = $sqlup->execute([$userId]);
        $row = $resultup->fetch();
        $resultup->closeCursor();

        if ($resultup && !$row) {

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

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

        } elseif ($resultup && $row) {

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

	    $sqlup = $this->connection->prepare('
			UPDATE `*PREFIX*sip_trip_phone`
			SET `pdisplayname` = ?, `sipusername` = ?, `sipuserpassword` = ?, `stphwssurl` = ?, `siprealm` = ?, `stunserver` = ?
                        WHERE `user_id` = ?');
	    $updateRes = $sqlup->execute([$pdisplayname, $sipusername, $sipuserpasswordenc, $stphwssurl, $siprealm, $stunserver, $userId]);
	    $updateRes->closeCursor();

        }
    }
}
