<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller as BaseController;

use App\Models\User;
use App\Models\Coin;
use App\Models\Coin_network;

use App\Services\AuthService;

use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use Illuminate\View\View;

use Arr;
use Carbon\Carbon;
use DB;
use Session;



class CoinController extends BaseController{
     
    function __construct(
        AuthService $authService
    ){
        $this->authService = $authService;
        $this->paginateLimit = system_settings('per_page');
    }
    
    /****
     * 
     * **/
    public function Coins(): View{
        $title = "Admin Dashboard";
        $user = auth()->user();
        $coins = Coin::orderBy('created_at','asc')->paginate($this->paginateLimit);
        return view('admin.coin.index' , [
            'title' => $title ,
            'user' => $user,
            'coins' => $coins
        ]);
    }
    
    public function addCoin(Request $request):RedirectResponse{
       
        
        $coinExists = Coin::where('name' , $request->name)->orWhere('symbol' , $request->sym)->first();
        if(!blank($coinExists)){
            return redirect()->back()
        ->with('errmessage', $coinExists->name. ' already exists');
        }
        $coin = new Coin();
        $coin->name = $request->name;
        $coin->symbol = $request->sym;
        $coin->allow_withdrawal = $request->awithdraw;
        $coin->allow_deposit = $request->adeposit;
        $coin->fee = $request->fee;
        $coin->fee_type = $request->ftype;
        $coin->status = $request->status;
        $coin->create_date = \Carbon\Carbon::now();
        
        $coin->save();
        return redirect()->back()
        ->with('sucmessage', $request->name. ' added sucessfully!');
  }
  
    public function editCoin(Request $request):RedirectResponse{
        $coinExists = Coin::where('id' , __get_hash($request->hashid))->first();
        if(empty($coinExists)){
            return redirect()->back()->with('errmessage' , $request->name.'  update failed');
        }
        
        $coin = array();
        
        $coin['name'] = $request->name;
        $coin['symbol'] = $request->sym;
        $coin['allow_withdrawal'] = $request->awithdraw;
        $coin['allow_deposit'] = $request->adeposit;
        $coin['fee'] = $request->fee;
        $coin['fee_type'] = $request->ftype;
        $coin['status'] = $request->status;
        
        Coin::where('id' , $coinExists->id)->update($coin);
        return redirect()->back()
        ->with('sucmessage', $coinExists->name. ' updated sucessfully!');
    }
  
    public function delCoin($token,$id):RedirectResponse{
        
        $coin = Coin::where('id' , __get_hash($id))->first();
        
        if(blank($coin)){
        return redirect()->back()->with('errmessage' , 'Coin not  found');
        }
  
        $coin->delete();
        $message = $coin->name . ' was deleted successfully';  
        return redirect()->back()
        ->with('sucmessage', $message);
    }
    
    
    /***
     * Coin Network
     * 
     * **/
     
    public function CoinNetwork($id = null): View{
        
        $title = "Admin Dashboard";
        $user = auth()->user();
        $coinId = '';
        $coin = '';
        
        
        $coinExists = Coin::where('id' , __get_hash($id))->first();
        
        
        if(!blank($coinExists)){
            $coin = $coinExists;
            $coinId =  $coinExists->id;
        }
        $coinNetwork = Coin_network::getAllNetwork($coinId)->paginate($this->paginateLimit);
        return view('admin.coin.network' , [
            'title' => $title ,
            'user' => $user,
            'coin' => $coinExists,
            'coinNetworks' => $coinNetwork
            
        ]);
    }
    
    public function addCoinNetwork(Request $request):RedirectResponse{
       
        
        $coinExists = Coin::where('id' , __get_hash($request->cvalue))->first();
        if(blank($coinExists)){
            return redirect()->back()
        ->with('errmessage', 'Coin does not exists');
        }
        $coinNetwork = Coin_network::where('network' , $request->name)->first();
        if(!blank($coinNetwork)){
            return redirect()->back()
        ->with('errmessage', $coinNetwork->network. ' already exists');
        }
        $wallet = explode(',' , $request->wallet);
        $address = json_encode($wallet);
        $coin = new Coin_network();
        $coin->coin = $coinExists->id;
        $coin->network = $request->name;
        $coin->network_source = $request->source;
        $coin->address = $wallet;
        $coin->status = $request->status;
        $coin->created_date = \Carbon\Carbon::now();
        //dd($coin);
        $coin->save();
        return redirect()->back()
        ->with('sucmessage', $request->name. ' network added sucessfully!');
  }
  
    public function editCoinNetwork(Request $request):RedirectResponse{
        $coinExists = Coin_network::where('id' , __get_hash($request->hashid))->first();
        //dd($coinExists);
        if(blank($coinExists)){
            return redirect()->back()->with('errmessage' , $request->name.'  update failed');
        }
        
        $wallet = explode(',' , $request->wallet);
        $address = json_encode($wallet);
        $coin = array();
        $coin['network'] = $request->name;
        $coin['network_source'] = $request->source;
        $coin['address'] = $wallet;
        $coin['status'] = $request->status;
        Coin_network::where('id' , $coinExists->id)->update($coin);
        return redirect()->back()
        ->with('sucmessage', $coinExists->name. ' updated sucessfully!');
    }
  
    public function delCoinNetwork($coin,$id):RedirectResponse{
        
        $coin = Coin_network::where('id' , __get_hash($id))->where('coin' , __get_hash($coin))->first();
        
        if(blank($coin)){
        return redirect()->back()->with('errmessage' , 'Network not  found');
        }
  
        $coin->delete();
        $message = $coin->network . ' network was deleted successfully';  
        return redirect()->back()
        ->with('sucmessage', $message);
    }
}