<?php

namespace App\Http\Controllers\Web;

use App\Models\Banner;
use App\Models\Page;
use App\Models\User;
use App\Models\Webhook;
use App\Services\Integration\Integration\Connection;
use App\Services\Integration\Integration\SMS;
use App\Services\Interfaces\BannerRepositoryInterface;
use App\Services\Interfaces\CategoryRepositoryInterface;
use App\Services\Interfaces\PageRepositoryInterface;
use App\Services\Interfaces\ProductRepositoryInterface;
use App\Services\Interfaces\RoomRepositoryInterface;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Config;
use Illuminate\View\View;
use App\Http\Controllers\Controller;
use App\Services\Interfaces\GroupRepositoryInterface;

class HomeController extends Controller
{
    private $view = 'web.pages.home.';
    private BannerRepositoryInterface $bannerRepository;
    private RoomRepositoryInterface $roomRepository;
    private ProductRepositoryInterface $productRepository;
    private CategoryRepositoryInterface $categoryRepository;
    private GroupRepositoryInterface $groupRepository;
    private PageRepositoryInterface $pageRepository;
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct(
        BannerRepositoryInterface $bannerRepository,
        RoomRepositoryInterface $roomRepository,
        ProductRepositoryInterface $productRepository,
        CategoryRepositoryInterface $categoryRepository,
        GroupRepositoryInterface $groupRepository,
        PageRepositoryInterface $pageRepository
    )
    {
        $this->bannerRepository = $bannerRepository;
        $this->roomRepository = $roomRepository;
        $this->productRepository = $productRepository;
        $this->categoryRepository = $categoryRepository;
        $this->groupRepository = $groupRepository;
        $this->pageRepository = $pageRepository;
    }

    /**
     * Show the application dashboard.
     * @return View
     */

    public function index(Request $request) :View
    {
        $title = __('web.home');
        // $banner = Banner::first();
        $banners = $this->bannerRepository->get($request);
        $request->merge(['pagination'=>4,'orderBy'=>'DESC','paginate'=>true]);
        $rooms = $this->roomRepository->get($request);
        // $menus = $this->categoryRepository->get($request);
        $request->merge(['active'=> 1, 'paginate'=> null, 'parents_only'=> true]);
        $menus = $this->groupRepository->get($request);
        // dd($menus->toArray());

        $products = $this->productRepository->get($request);
        $request->merge(['ids'=>[1,2,3,4],'paginate'=>null]);
        $cards = $this->pageRepository->get($request);
        $page = Page::find(5);
        $page = $this->pageRepository->getItem($page);
        
        
        $section = Page::find(10);
        $section = $this->pageRepository->getItem($section);
        
        $card1 = Page::find(11);
        $card1 = $this->pageRepository->getItem($card1);
        
        $card2 = Page::find(12);
        $card2 = $this->pageRepository->getItem($card2);

        return view($this->view.'index', get_defined_vars());
    }
    public function test()
    {
        $connection = new Connection();
        $url = Config::get('app.baseUrl').'categories';
        $data = $connection->get($url,[]);
        if ($data->successful())
        {
            $data = json_decode($data->body());
            foreach ($data->data as $row)
            {
                $this->categoryRepository->create($row);
            }
        }
    }

    public function testPhone($phone)
    {
        $code = rand(11111,99999);
        $message = "Your Code : ".$code;
        SMS::sendSMS($phone,$message);
        // dd($response_json);

    }
    public function senderName()
    {

        $app_id = "SVkSuJ5a5VfMOCiK1WlLLiZGbRmi8la5xmivraZd";
        $app_sec = "5yleIBACQMPSMsUF1cK1vY71dqvl4EqaogAXg8w8NYi7idWc7q3ahjn4Kd2NzA8gOhuEWGkALglhwEDgTu547sQ9UyInpOLYmqxH";
$app_hash  = base64_encode("$app_id:$app_sec");
$base_url = "https://api-sms.4jawaly.com/api/v1/";
$query = [];
$query["is_active"] = 1; // get active only
$query["order_by"] = "id"; // package_points, current_points, expire_at or id (default)
$query["order_by_type"] = "desc"; // desc or asc
$query["page"] = 1 ;
$query["page_size"] = 10 ;
$query["return_collection"] = 1; // if you want to get all collection
$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_URL => $base_url.'account/area/me/packages?'.http_build_query($query),
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'GET',
    CURLOPT_HTTPHEADER => array(
        'Accept: application/json',
        'Content-Type: application/json',
        'Authorization: Basic '.$app_hash
    ),
));

$response = curl_exec($curl);

curl_close($curl);

// var_dump();
dd(json_decode($response));

        $app_id = "SVkSuJ5a5VfMOCiK1WlLLiZGbRmi8la5xmivraZd";
        $app_sec = "5yleIBACQMPSMsUF1cK1vY71dqvl4EqaogAXg8w8NYi7idWc7q3ahjn4Kd2NzA8gOhuEWGkALglhwEDgTu547sQ9UyInpOLYmqxH";
        $app_hash  = base64_encode("$app_id:$app_sec");
        $base_url = "https://api-sms.4jawaly.com/api/v1/";
        $query = [];
        $query["page_size"] = 10; // if you want pagination how many items per page
        $query["page"] = 1;// page number
        $query["status"] = 1; // get active 1 in active 2
        $query["sender_name"] = ''; // search sender name full name
        $query["is_ad"] = ''; // for ads 1 and 2 for not ads
        $query["return_collection"] = 1; // if you want to get collection for all not pagination
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => $base_url.'account/area/senders?'.http_build_query($query),
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'GET',
            CURLOPT_HTTPHEADER => array(
                'Accept: application/json',
                'Content-Type: application/json',
                'Authorization: Basic '.$app_hash
            ),
        ));

        $response = curl_exec($curl);

        curl_close($curl);

        var_dump(json_decode($response));
    }
    
    public function testPayment(Request $request)
    {
        $webhook = Webhook::find(874)->body;
        $response = json_decode($webhook,true);
        if($response['EventType'] == 1)
        {
            if(isset($response['Data']))
            {
                if(isset($response['Data']['InvoiceId']) && ($response['Data']['TransactionStatus'] == 'SUCCESS' ?? false))
                {
                    dd($response);
                }
            }
        }
        dd('failed');
    }
}
