<?php
// require_once('basic-function.php');
// use wallet;
class deposit extends basicFunction
{
    use table_names;

    // public function __construct(wallet $wallet) {
    //     $this->wallet = $wallet;    
    // }

    public function create($investment, $currency, $package, $amt)
    {
        $vals = [$investment, $currency, $package, $amt];
        $if_empty = $this->if_empty($vals);
        if (!$if_empty) {
            return $this->errorMsgs(1);
        }
        $user = $this->selectWhereDlt($this->user_tb,'unique_id',$_SESSION[$this->user_session])[0];

        $investment_exist = $this->selectWhereDlt($this->investment_plan_tb, 'unique_id', $investment);
        if (count($investment_exist) < 1) {
            return $this->errorMsgs(15);
        }
        $currency_exist = $this->selectWhereDlt($this->currencies_tb, 'unique_id', $currency);
        if (count($currency_exist) < 1) {
            return $this->errorMsgs(15);
        }

        $user_address = $this->selectWhere2CondDlt($this->user_wallet_tb,'user_id',$user['unique_id'],'coin_id',$currency);
        if(empty($user_address[0]['address'])){
            return $this->errorMsgs(22);
        }

        $package_exist = $this->selectWhereDlt($this->package_tb, 'unique_id', $package);
        if (count($package_exist) < 1) {
            return $this->errorMsgs(15);
        }
        if($amt < $package_exist[0]['min_amt'] || $amt > $package_exist[0]['max_amt']){
            return ['error'=>1,'msg'=>'Error! Amount is below or above package limit!.'];
        }

        $response = $this->api_call('https://min-api.cryptocompare.com/data/price?fsym=' . $currency_exist[0]['symbol'] . '&tsyms=USD&api_key=' . $this->crypto_compare_api_key);

        if (empty($response))  {
            return $this->errorMsgs(15);
        }
        $amt_in_coin = $this->convert_to_crypto($response,$amt);

            $unique_id = $this->rand_id();
            $date_created = $this->todayDate();
            $query = "INSERT INTO $this->transaction_tb VALUES (null,
        '" . mysqli_real_escape_string($this->connectDb(), $unique_id) . "',
        '" . mysqli_real_escape_string($this->connectDb(), $user['unique_id']) . "',
        '" . mysqli_real_escape_string($this->connectDb(), $investment) . "',
        '" . mysqli_real_escape_string($this->connectDb(), $currency) . "',
        '" . mysqli_real_escape_string($this->connectDb(), $package) . "',
        '" . mysqli_real_escape_string($this->connectDb(), $amt) . "',
        '" . mysqli_real_escape_string($this->connectDb(), $amt_in_coin) . "',
        '" . mysqli_real_escape_string($this->connectDb(), 'pending') . "',
        '" . mysqli_real_escape_string($this->connectDb(), null) . "',
        '" . mysqli_real_escape_string($this->connectDb(), 0) . "',
        '" . mysqli_real_escape_string($this->connectDb(), 0) . "',
        '" . mysqli_real_escape_string($this->connectDb(), 0) . "',
        '" . mysqli_real_escape_string($this->connectDb(), $date_created) . "',
        '" . mysqli_real_escape_string($this->connectDb(), $date_created) . "') ";
            mysqli_close($this->connectDb());
            if ($this->sqlQuery($query)) {
                return [
                    'error' => 0,
                    'txn_id' => $unique_id,
                    'site_root' => $this->the_site_root,
                ];
            } else {
                return $this->errorMsgs(14);
            }
    }

    public function confirm($id)
    {
        $vals = [$id];
        $if_empty = $this->if_empty($vals);
        if (!$if_empty) {
            return $this->errorMsgs(15);
        }
        $deposit_exist = $this->selectWhereDlt($this->transaction_tb, 'unique_id', $id);
        if (count($deposit_exist) < 1) {
            return $this->errorMsgs(15);
        }
        if($deposit_exist[0]['status'] == 'completed'){
            return ['error'=>1,'msg'=> 'Transaction has already been completed.'];
        }
        $date_created = $this->todayDate();
        $fields = ['status','date_confirmed'];
        $values = ['confirmed',$date_created];
        $update_txn = $this->update_tbs( $this->transaction_tb,$fields,$values,$id);
        if($update_txn['error'] == 1){
            return $update_txn;
        }

        $is_referred = $this->selectWhereDlt($this->user_details,'user_unique_id',$deposit_exist[0]['user_id'])[0];
        if(!empty($is_referred['referrer'])){
            $percentage = $this->calc_referr_percentage($deposit_exist[0]['amt_in_usd']);
            $unique_id = $this->rand_id();
            $query = "INSERT INTO $this->referrer_bonus_tb VALUES (null,
            '".mysqli_real_escape_string($this->connectDb(),$unique_id)."',
            '".mysqli_real_escape_string($this->connectDb(),$deposit_exist[0]['unique_id'])."',
            '".mysqli_real_escape_string($this->connectDb(),$is_referred['referrer'])."',
            '".mysqli_real_escape_string($this->connectDb(),$deposit_exist[0]['user_id'])."',
            '".mysqli_real_escape_string($this->connectDb(),$percentage)."',
            '".mysqli_real_escape_string($this->connectDb(),$deposit_exist[0]['coin_type_id'])."',
            '".mysqli_real_escape_string($this->connectDb(),0)."',
            '".mysqli_real_escape_string($this->connectDb(),$date_created)."',
            '".mysqli_real_escape_string($this->connectDb(),$date_created)."') ";
            mysqli_close($this->connectDb());
            if($this->sqlQuery($query)){
                return $this->successMsg2('Deposit Confirmation');
            }else{
                return $this->errorMsgs(14);
            }
        }
        return $this->successMsg2('Deposit Confirmation');

    }

    public function end_txn($id)
    {
        $vals = [$id];
        $if_empty = $this->if_empty($vals);
        if (!$if_empty) {
            return $this->errorMsgs(15);
        }
        $txn_exist = $this->selectWhereDlt($this->transaction_tb, 'unique_id', $id);
        if (count($txn_exist) < 1) {
            return $this->errorMsgs(15);
        }
        $date = $this->todayDate();
        $fields = ['status','date_confirmed'];
        $values = ['completed',$date];
        $update_txn = $this->update_tbs( $this->transaction_tb,$fields,$values,$id);
        if($update_txn['error'] == 1){
            return $update_txn;
        }
        return $this->successMsg3('Transaction Ended');
    }

    public function unconfirm($id)
    {
        $vals = [$id];
        $if_empty = $this->if_empty($vals);
        if (!$if_empty) {
            return $this->errorMsgs(15);
        }
        $deposit_exist = $this->selectWhereDlt($this->transaction_tb, 'unique_id', $id);
        if (count($deposit_exist) < 1) {
            return $this->errorMsgs(15);
        }
        $fields = ['status','date_confirmed'];
        $values = ['pending',null];
        $update_txn = $this->update_tbs( $this->transaction_tb,$fields,$values,$id);
        if($update_txn['error'] == 1){
            return $update_txn;
        }

        $is_referred = $this->selectWhereDlt($this->referrer_bonus_tb,'txn_id',$id);
        if(count($is_referred) > 0){
            $fields = ['is_deleted'];
            $values = ['1'];
            $update_referr = $this->update_where( $this->referrer_bonus_tb,$fields,$values,'txn_id',$id);
            if($update_referr['error'] == 1){
                return $update_referr;
            }
        }
        return $this->successMsg3('Transaction Unconfirmed');
    }

    public function delete($id)
    {
        $vals = [$id];
        $if_empty = $this->if_empty($vals);
        if (!$if_empty) {
            return $this->errorMsgs(15);
        }
        $fields = ['is_deleted'];
        $values = ['1'];
        $delete_txn = $this->update_tbs( $this->transaction_tb,$fields,$values,$id);
        if($delete_txn['error'] == 1){
            return $delete_txn;
        }
        $is_referred = $this->selectWhereDlt($this->referrer_bonus_tb,'txn_id',$id);
        if(count($is_referred) > 0){
            $fields = ['is_deleted'];
            $values = ['1'];
            $update_referr = $this->update_where( $this->referrer_bonus_tb,$fields,$values,'txn_id',$id);
            if($update_referr['error'] == 1){
                return $update_referr;
            }
        }
        return $this->dltSuccessMsg();
    }

    public function edit($id,$status,$interest)
    {
        $vals = [$status];
        $if_empty = $this->if_empty($vals);
        if (!$if_empty) {
            return $this->errorMsgs(1);
        }
        if(empty($interest)){
            $interest = 0;
        }
        $vals = [$id];
        $if_empty = $this->if_empty($vals);
        if (!$if_empty) {
            return $this->errorMsgs(15);
        }

        if($status == 'pending'){
            $unconfirm = $this->unconfirm($id);
            if($unconfirm['error'] == 1){
                return $unconfirm;
            }
        }
        if($status == 'confirmed'){
            $confirm = $this->confirm($id);
            if($confirm['error'] == 1){
                return $confirm;
            }
        }
        if($status == 'completed'){
            $completed = $this->end_txn($id);
            if($completed['error'] == 1){
                return $completed;
            }
        }
        $fields = ['interest_in_usd'];
        $values = [$interest];
        $update_interest = $this->update_where( $this->transaction_tb,$fields,$values,'unique_id',$id);
        if($update_interest['error'] == 1){
            return $update_interest;
        }
        return $this->successMsg2('Transaction Edit');

    }

    /**
     * Function to convert crypto_compare response to crypto equivalent.
     *
     * 
     */
    public function convert_to_crypto($response, $amt_in_usd)
    {
        // the api returns the equivalent of 1 of the coin in usd
        $equiv = $response['USD'];
        $amt_in_coin = $amt_in_usd / $equiv;
        return $amt_in_coin;
    }



}
$deposit = new deposit();
