<?php

namespace App\Core\Service;

use App\Core\Repository\PropertyWebMenu\PropertyWebMenuRepository;

use App;
use Illuminate\Support\Facades\Log;
use Exception;
use App\Exceptions\ApiErrorException;

class PropertyWebMenuService
{

    private $propertyWebMenuRepository;



    public function __construct(
        PropertyWebMenuRepository $propertyWebMenuRepository
    )
    {

        $this->propertyWebMenuRepository = $propertyWebMenuRepository;

    }

    public function select($param = [], $column = ['*'])
    {

        $response = ['status' => -1, 'message' => '', 'data' => null];

        try {
            $data = $this->propertyWebMenuRepository->findByCriteria($param, $column);
            $response = [
                'status' => true,
                'data' => $data,
            ];
        } catch (ApiErrorException $e) {
            $response['message'] = $e->getMessage();
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
        }

        return output($response);
    }

    public function getPropertyWebMenu(){

        $response = ['status' => false, 'message' => '', 'data' => null, 'statusCode' => 500];
        try {

            $return = [];
            $searchCriteria = [
                'criteria' => [['field' => 'status', 'condition' => '=', 'value' => 1]],
                'orderBy' => [["field" => "order_number", "value" => "ASC"]],
            ];

            $searchResult = $this->select($searchCriteria, ['id', 'name', 'language_key', 'route', 'type', 'order_number', 'icon', 'alias']);

            if($searchResult['status'] != 'success'){
                throw new ApiErrorException($searchResult['message']);
            }

            $webMenus = collect($searchResult['data'])->map(function ($value){
                $return = $value ;
                $return['menu_type'] = 'MAIN' ;
                return $return ;
            })->values()->all() ;
            $return = $webMenus;

            $response = ['statusCode' => 200, 'status' => true, 'message' => '', 'data' => ['property_web_menus' => $return ] ];
        } catch (ApiErrorException $e) {
            $response['message'] = implode(', ', $e->getMessageArr());
            $response['statusCode'] = 400;
        } catch (Exception $e) {
            $message = $e->getFile() . " " . $e->getLine() . " " . $e->getMessage();
            Log::error($message);
            $response['message'] = $e->getMessage();
            $response['statusCode'] = 500;
        }

        return output($response);
    }




}
