<?php

namespace App\Admin\Services;

use App\Admin\Repositories\SiteAlbumFolder;
use App\Admin\Repositories\SiteAlbum;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;

/**
 * 外部rpc服务
 */
class RpcService
{
    protected $request;
    protected $apiSecret = 'MtbSecretVBUC';


    /*
     * 构造函数,验证签名
     */
    public function __construct()
    {
        try {
            $request = request();
            // 验证签名
            $apiKey = $request->header('X-API-Key');
            $clientSignature = $request->header('X-API-Signature');
            $all = $request->all();
            $params = isset($all['params'])? $all['params'] : [];
            $payload = json_encode($params);
            $payload = $payload.$apiKey;
            $serverSignature = hash_hmac('sha256', $payload, $this->apiSecret);
            if ($clientSignature !== $serverSignature) {
                return $this->responseError('签名验证失败');
            }
            if (time() - $apiKey > 100) {
                return $this->responseError('签名已过期');
            }
        } catch (\Exception $e) {
            return $this->responseError('签名验证失败 e - '. $e->getMessage());
        }
    }

    /*
     * 相册分页
     * @param int $page 页码
     * @param int $perPage 每页数量
     */
    public function siteAlbumPaginate($filter = [],$sort = [],$perPage = 15,$page = 1): string
    {
        $result = SiteAlbum::siteAlbumPaginate($filter,$sort,$perPage,$page);
        $result = $result->toArray();
        return $this->responseSuccess( $result);
    }

    /*
     * 通过ids获取相册列表
     */
    public function siteAlbumGetByIds($ids): string
    {
        if (is_string($ids)) {
            $ids = explode(',', $ids);
        }
        $siteAlbum = new SiteAlbum();
        $result = $siteAlbum->model()->whereIn('id', $ids)->get();
        $result = $result->toArray();
        return $this->responseSuccess( $result);
    }

    /*
     * 相册详情
     */
    public function siteAlbumGet($id)
    {
        $siteAlbum = new SiteAlbum();
        $result = $siteAlbum->model()->find($id);
        if (!$result) {
            return $this->responseError('相册不存在');
        }
        $result = $result->toArray();
        return $this->responseSuccess( $result);
    }


    /*
     * 相册文件夹列表
     * $inIds 查询指定id及父级id的相册文件夹
     */
    public function siteAlbumFolderSelectOptions($inIds = [])
    {
        $result = SiteAlbumFolder::siteAlbumFolderSelectOptions($inIds);
        return $this->responseSuccess( $result);
    }


    public function siteAlbumFolderSelectOptionsEn($inIds = [])
    {
        $result = SiteAlbumFolder::siteAlbumFolderSelectOptionsEn($inIds);
        return $this->responseSuccess( $result);
    }


    /*
     * 全部相册文件夹列表
     */
    public function siteAlbumFolderSelectOptionsAll($lang = 'en')
    {
        $result = SiteAlbumFolder::siteAlbumFolderSelectOptionsAll($lang);
        return $this->responseSuccess( $result);
    }


    /*
     * 相册文件夹全部节点
     */
    public function siteAlbumFolderAllNodes()
    {
        $siteAlbumFolder = new SiteAlbumFolder();
        $result = $siteAlbumFolder->model()->allNodes();
        return $this->responseSuccess($result);
    }

    /*
     * rcp服务响应错误信息
     */
    public function responseError($result) {
        $result = ['status'=>false,'msg'=>$result,'data'=>[]];
        $response = ["jsonrpc"=>"2.0", "result"=> $result, "id"=>1];
        die(json_encode($response));
    }


    /*
     * rcp服务响应成功信息
     */
    public function responseSuccess($data) {
        $result = ['status'=>true,'msg'=>'Success','data'=>$data];
        $response = ["jsonrpc"=>"2.0", "result"=> $result, "id"=>1];
        die(json_encode($response));
    }
}
