<?php

namespace App\Http\Controllers\V1;

use App\Helper\ByteDance;
use App\Helper\Kuaishou;
use App\Helper\UniPlatform\Bytedance\ByteDanceAPI;
use App\Helper\UniPlatform\Kuaishou\KuaishouAPI;
use App\Helper\UniPlatform\Wechat\WechatAPI;
use App\Helper\Wechat;
use App\Models\PayConfig;
use Dingo\Api\Routing\Helpers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use App\Services\Base\ErrorCode;

class Controller extends BaseController
{
    use AuthorizesRequests;
    use DispatchesJobs;
    use ValidatesRequests;
    use Helpers;

    public function success($data = null, $code = 0, $msg = null)
    {
        if (!$msg) {
            $msg = trans('api.SUCCESS');
        }

        $result = [
            'code'    => $code,
            'message' => $msg,
            'data'    => $data,
        ];

        return response()->json($result);
    }

    public function error($msg = null, $code = 1, $data = null)
    {
        $result = [
            'code'    => $code,
            'message' => $msg,
            'data'    => $data,
        ];

        return response()->json($result);
    }

    public function validatorError($arr, $code = 1, $message = '')
    {
        foreach ($arr as $val) {
            if ($val && '' == $message) {
                $message = $val;
            }
        }
        if (0 !== $code && ErrorCode::CLIENT_WRONG_PARAMS && empty($msg)) {
            $msg = ErrorCode::message($code);
        }
        $result = [
            'code'    => $code,
            'message' => $msg,
            'data'    => $data,
        ];

        return response()->json($result);
    }

    /**
     * @return ByteDance|Kuaishou|Wechat
     */
    protected function getUniFactory(int $platform = 1)
    {
        if (1 == $platform) {
            return $this->getByteDanceFactory();
        } elseif (2 == $platform) {
            return $this->getKuishouFactory();
        }

        return $this->getWechatFactory();
    }

    protected function getByteDanceFactory(): ByteDance
    {
        $setting = PayConfig::first();

        return (new ByteDance(app(ByteDanceAPI::class)))->factory([
            'app_id'     => $setting->douyin_app_id,
            'app_secret' => $setting->douyin_app_secret,
            'slat'       => $setting->douyin_salt,
            'token'      => $setting->douyin_token,
        ]);
    }

    protected function getKuishouFactory(): Kuaishou
    {
        $setting = PayConfig::first();

        return (new Kuaishou(app(KuaishouAPI::class)))->factory([
            'app_id'     => $setting->kuaishou_app_id,
            'app_secret' => $setting->kuaishou_app_secret,
        ]);
    }

    protected function getWechatFactory(): Wechat
    {
        $setting = PayConfig::first();

        return (new Wechat(app(WechatAPI::class)))->factory([
            'app_id'        => $setting->mini_app_id,
            'app_secret'    => $setting->mini_app_key,
            'mch_id'        => $setting->wechat_mch_id,
            'mch_key'       => $setting->wechat_mch_key,
            'response_type' => 'array',
        ]);
    }
}
