<?php
/**
 * Created by PhpStorm.
 * User: qiuzijian
 * Date: 2021-04-25
 * Time: 09:53
 */

namespace Modules\Camera\Http\Controllers\Api;

use App\Enum\ApiEnum;
use App\Http\Controllers\Api\BaseController;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Log;
use Modules\Camera\Entities\CameraList;
use Modules\Camera\Enum\CameraEnum;
use Modules\Camera\Services\CameraServices;
use Modules\Mine\Entities\MineListExt;
use DB;

class CameraApiController extends BaseController
{
    /**
     * 作者: qiuzijian
     * 注释: 获取摄像头列表
     * @return \App\Http\Controllers\Api\JsonResponse
     */
    public function getCamerasList()
    {
        $parent_id   = Input::get('parent_id', '');
        $mine_id     = Input::get('mine_id', '');
        $camera_type = Input::get('camera_type', CameraEnum::CAMERA_TYPE_ALL);
        $surface_id  = Input::get('surface_id', '');

        if (!$parent_id) {
            return self::errorResponse(ApiEnum::STATUS_CODE_EMPTY);
        }

        if (!in_array($camera_type, [CameraEnum::CAMERA_TYPE_NORMAL, CameraEnum::CAMERA_TYPE_ALL]) && !$surface_id) {
            return self::errorResponse(ApiEnum::STATUS_CODE_EMPTY);
        }

        //判断是否使用海康视频服务器
//        $is_hak = MineListExt::where('mine_id', $parent_id)->value('is_hak');

//        if ($is_hak) {
//            $result = CameraServices::getHaiKangCamera($parent_id, $mine_id, $camera_type);
//        } else {
            $result = CameraServices::getCameraListByMineId($mine_id, $camera_type, $parent_id, $surface_id);
//        }

        return self::successResponse($result);
    }

    /**
     * 作者: qiuzijian
     * 注释: 获取摄像头播放地址
     * @return \App\Http\Controllers\Api\JsonResponse
     */
    public function getCamerasUrl()
    {
        $parent_id = Input::get('parent_id', '');
        $camera_id = Input::get('camera_id', '');

        if (!$parent_id || !$camera_id) {
            return self::errorResponse(ApiEnum::STATUS_CODE_EMPTY);
        }

        //判断摄像头来源是否为内网服务器
        $camera_source = CameraList::where('id', $camera_id)->value('camera_source');

        if ($camera_source == CameraEnum::CAMERA_SOURCE_3) {
            $result = CameraServices::getIntranetCameraUrl($camera_id);
            return self::successResponse($result);
        }

        //判断是否使用海康视频服务器
        $is_hak = MineListExt::where('mine_id', $parent_id)->value('is_hak');

        //判断是否生成摄像头请求链接
        $camera_url = CameraList::where('id', $camera_id)->value('camera_url');

        if ($is_hak && !$camera_url) {
            //判断是否是mine配置文件的矿区
            $mine_res = DB::table('mine_list')->where('id',$parent_id)->get();
            if(isset($mine_res[0]->title) && in_array($mine_res[0]->title,config('mine'))){
                $result = CameraServices::getRtspTianDi($camera_id,$parent_id);
            }else{
                $result = CameraServices::downloadCameraFiles($parent_id, $camera_id);
            }
        } else {
            //判断硬盘录像机的有没有封面图，有的走easydrawin，没有的走ffmpeg
            $cover_picture = CameraList::where('id', $camera_id)->value('cover_picture');
//            if(!$cover_picture){
                //ffmpeg拉流获取首帧图片
//                $result = CameraServices::getCameraUrlByFfmpeg($camera_id);
//            }else{
                $result = CameraServices::getCameraUrlByCameraId($camera_id);
//            }
        }

        return self::successResponse($result);
    }

    //获取海康rtsp流
    public function getHikRtsp(){
        $parent_id = Input::get('parent_id', '');
        $camera_id = Input::get('camera_id', '');

        if (!$parent_id || !$camera_id) {
            return self::errorResponse(ApiEnum::STATUS_CODE_EMPTY);
        }

        //判断是否使用海康视频服务器
        $is_hak = MineListExt::where('mine_id', $parent_id)->value('is_hak');

        //判断是否生成摄像头请求链接
        $camera_url = CameraList::where('id', $camera_id)->value('camera_url');

        if ($is_hak == 1 && !$camera_url) {
            $result = CameraServices::getHkRtsp($camera_id,$parent_id);
            return self::successResponse($result);
        }else{
            return self::errorResponse(ApiEnum::HK_REQUEST_FAIL);
        }
    }

    //海康摄像头easy_drawin方式获取url
    public function getCamerasRtsp()
    {
        $parent_id = Input::get('parent_id', '');
        $camera_id = Input::get('camera_id', '');

        if (!$parent_id || !$camera_id) {
            return self::errorResponse(ApiEnum::STATUS_CODE_EMPTY);
        }

        //判断摄像头来源是否为内网服务器
        $camera_source = CameraList::where('id', $camera_id)->value('camera_source');

        if ($camera_source == CameraEnum::CAMERA_SOURCE_3) {
            $result = CameraServices::getIntranetCameraUrl($camera_id);
            return self::successResponse($result);
        }

        //判断是否使用海康视频服务器
        $is_hak = MineListExt::where('mine_id', $parent_id)->value('is_hak');

        //判断是否生成摄像头请求链接
        $camera_url = CameraList::where('id', $camera_id)->value('camera_url');

        if ($is_hak && !$camera_url) {
            //判断是否是mine配置文件的矿区
            $mine_res = DB::table('mine_list')->where('id',$parent_id)->get();
            if(isset($mine_res[0]->title) && in_array($mine_res[0]->title,config('mine'))){
                //天地伟业使用easy_drawin获取rtsp
                $result = CameraServices::getRtspTianDi($camera_id,$parent_id);
            }else{
                //海康使用easy_drawin获取rtsp
                $result = CameraServices::getRtspHaiKang($camera_id, $parent_id);
            }
        } else {
            //硬盘录像机使用easy_drawin获取rtsp
            $result = CameraServices::getRtspYingPan($camera_id);
        }

        return self::successResponse($result);
    }



    /**
     * 作者: qiuzijian
     * 注释: 停止摄像头推流
     * @return \App\Http\Controllers\Api\JsonResponse
     */
    public function stopCamerasStream()
    {
        $camera_id = Input::get('camera_id', '');

        $result = CameraServices::stopCameraStream();

        return self::successResponse($result);
    }

    /**
     * 作者: qiuzijian
     * 注释: 获取编码设备信息
     * @return \App\Http\Controllers\Api\JsonResponse
     */
    public function getTranscodeInfo()
    {
        $parent_id = Input::get('parent_id', '');

        if (!$parent_id) {
            return self::errorResponse(ApiEnum::STATUS_CODE_EMPTY);
        }

        $result = CameraServices::getHaiKangTransCode($parent_id);

        return self::successResponse($result);
    }

    /**
     * 作者: qiuzijian
     * 注释: 获取大南湖摄像头列表
     * @return \App\Http\Controllers\Api\JsonResponse
     */
    public function getSouthLakeCamera()
    {
        $result = CameraServices::getLeChangeCamera();

        return self::successResponse($result);
    }
}