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

    public function create($name,$address,$symbol)
    {
        $vals = [$name,$address,$symbol];
        $if_empty = $this->if_empty($vals);
        if (!$if_empty) {
            return $this->errorMsgs(1);
        }

        $name_exist = $this->selectWhereDlt($this->currencies_tb,'name',$name);
        if(count($name_exist) > 0){
            return $this->errorMsgs(16);
        }
        $address_exist = $this->selectWhereDlt($this->currencies_tb,'admin_wallet_address',$address);
        if(count($address_exist) > 0){
            return ['error'=>1,'msg'=>'Error! Wallet Address has been used previously!.'];
        }
        
        $unique_id = $this->rand_id();
        $date_created = $this->todayDate();
        $query = "INSERT INTO $this->currencies_tb VALUES (null,
        '".mysqli_real_escape_string($this->connectDb(),$unique_id)."',
        '".mysqli_real_escape_string($this->connectDb(),$name)."',
        '".mysqli_real_escape_string($this->connectDb(),$address)."',
        '".mysqli_real_escape_string($this->connectDb(),$symbol)."',
        '".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)){
            
            $user_wallet = $this->create_wallet($unique_id,$unique_id);
            if($user_wallet['error'] == 1){
                return $user_wallet;
            }

            return $this->successMsg('Currency');
        }else{
            return $this->errorMsgs(14);
        }

    }
    public function create_wallet($user_id,$coin_id)
    {
        $vals = [$user_id,$coin_id];
        $if_empty = $this->if_empty($vals);
        if (!$if_empty) {
            return $this->errorMsgs(15);
        }
        $all_users = $this->selectAllDlt($this->user_tb);
        if(count($all_users) < 1){
            return $this->successMsg2('Wallet Added');
        }

        foreach ($all_users as $e ) { 
            $insert = $this->insert_user_wallet($e['unique_id'],$coin_id);
            if($insert['error'] == 1){
                return $insert;
            }
        }
        return $this->successMsg('Wallet');

    }
    
    public function insert_user_wallet($user_id,$coin_id)
    {        
        $unique_id = $this->rand_id();
        $date_created = $this->todayDate();
        $query = "INSERT INTO $this->user_wallet_tb VALUES (null,
        '".mysqli_real_escape_string($this->connectDb(),$unique_id)."',
        '".mysqli_real_escape_string($this->connectDb(),$user_id)."',
        '".mysqli_real_escape_string($this->connectDb(),$coin_id)."',
        '".mysqli_real_escape_string($this->connectDb(),null)."',
        '".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)){
        }else{
            return $this->errorMsgs(14);
        }
    }

    public function edit($id,$name,$symbol,$address)
    {
        $vals = [$id];
        $if_empty = $this->if_empty($vals);
        if (!$if_empty) {
            return $this->errorMsgs(15);
        }
        $vals = [$name,$symbol,$address];
        $if_empty = $this->if_empty($vals);
        if (!$if_empty) {
            return $this->errorMsgs(1);
        }
        $fields = ['name','admin_wallet_address','symbol'];
        $values = [$name,$address,$symbol];
        $update_coin = $this->update_where( $this->currencies_tb,$fields,$values,'unique_id',$id);
        if($update_coin['error'] == 1){
            return $update_coin;
        }
        return $this->successMsg2('Wallet Edit');
    }

    public function delete($id)
    {
        $vals = [$id];
        $if_empty = $this->if_empty($vals);
        if (!$if_empty) {
            return $this->errorMsgs(15);
        }
        $fields = ['is_deleted'];
        $values = ['1'];
        $currency_update = $this->update_tbs( $this->currencies_tb,$fields,$values,$id);
        if($currency_update['error'] == 1){
            return $currency_update;
        }
        $fields = ['is_deleted'];
        $values = ['1'];
        $wallet_update = $this->update_where( $this->user_wallet_tb,$fields,$values,'coin_id',$id);
        if($wallet_update['error'] == 1){
            return $wallet_update;
        }
        return $this->dltSuccessMsg();
    }
    
}
$currency = new currency();