<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Throwable;
use Exception;
use Carbon\Carbon;
use Illuminate\Validation\Rule;
use App\Http\Controllers\BaseController;
use App\Services\Apk\IApkService;

class ApkController extends BaseController
{
    private $time;
    private $apkService;

    public function __construct(IApkService $apkService)
    {
        $this->time = Carbon::now()->format('Y-m-d H:i:s');
        $this->apkService = $apkService;
    }

    /**
     * apk包列表
     * @return array
     * @date 2020/11/19
     * @author live
     */
    public function getApkList(Request $request)
    {
        $name = $request->get('name', '');
        $data['LISTS'] =$this->apkService->getApkList($name);
        $data['NAME'] = $name;
        return view('apk.list', $data);
    }

    /**
     * 上传apk
     * @return array
     * @date 2020/11/19
     * @author live
     */
    public function getApkAdd(Request $request)
    {
        $postData = $request->all();
        $apk = $postData['apk'] ?? '';
        if($apk == '') return $this->getErrorMsg(3001, config('msg.apk.3001'));
        $validate = Validator::make($postData, [
            'name' => 'required|string',
            'version' => 'required|string'
        ]);
        if ($validate->fails()) return $this->getErrorMsg(1001, config('msg.common.1001'));

        try {
            $data = $this->apkService->getApkAdd($postData);
            return $this->getSuccessMsg($data);
        } catch (Exception $e) {
            return $this->getErrorMsg($e->getCode(), $e->getMessage());
        }
    }

    /**
     * 设置apk包状态
     * @return array
     * @date 2020/11/26
     * @author live
     */
    public function getApkSetStatus(Request $request)
    {
        $postData = $request->all();
        $validate = Validator::make($postData, [
            'id' => 'required|numeric',
            'status' => 'required|numeric'
        ]);
        if ($validate->fails()) return $this->getErrorMsg('1001', config('msg.common.1001'));
        try {
            $data = $this->apkService->getApkSetStatus($postData);
            return $this->getSuccessMsg($data);
        } catch (Exception $e) {
            return $this->getErrorMsg($e->getCode(), $e->getMessage());
        }
    }

    /**
     * 删除apk包
     * @return array
     * @date 2020/11/26
     * @author live
     */
    public function getApkDel(Request $request)
    {
        $postData = $request->all();
        $validate = Validator::make($postData, [
            'id' => 'required|numeric'
        ]);

        if ($validate->fails()) return $this->getErrorMsg('1001', config('msg.common.1001'));
        try {
            $data = $this->apkService->getApkDel($postData['id']);
            return $this->getSuccessMsg($data);
        } catch (Exception $e) {
            return $this->getErrorMsg($e->getCode(), $e->getMessage());
        }
    }

    /**
     * 修改apk包
     * @return array
     * @date 2020/11/26
     * @author live
     */
    public function getApkEdit(Request $request)
    {
        $postData = $request->all();
        $validate = Validator::make($postData, [
            'name' => 'required|string',
            'version' => 'required|string'
        ]);
        if ($validate->fails()) return $this->getErrorMsg(1001, config('msg.common.1001'));

        try {
            $data = $this->apkService->getApkEdit($postData);
            return $this->getSuccessMsg($data);
        } catch (Exception $e) {
            return $this->getErrorMsg($e->getCode(), $e->getMessage());
        }

    }
    /**
     * 下载apk
     * @return array
     * @date 2020/11/26
     * @author live
     */
    public function getApkDown(Request $request)
    {
       $md5 = $request->route('md5','');
       if($md5 == '') abort(404);
        try {
            $downUrl = $this->apkService->getApkDown($md5);
            if($downUrl){
                header('location:'.$downUrl);
                exit();
            }
        } catch (Exception $e) {
            abort(404);
        }
        abort(404);
    }
}

