package com.brframework.webapppatch.web;

import com.brframework.commonweb.exception.HandleException;
import com.brframework.commonweb.json.JSONResult;
import com.brframework.webapppatch.domain.AppPatchConfig;
import com.brframework.webapppatch.entity.PatchBranch;
import com.brframework.webapppatch.json.AppPatchBranchBasePackageParam;
import com.brframework.webapppatch.json.AppPatchReleaseVersionParam;
import com.brframework.webapppatch.service.PatchBranchService;
import com.brframework.webapppatch.service.PatchConfigService;
import com.brframework.webapppatch.service.PatchVersionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author xu
 * @date 2019/11/14 17:02
 */
@RestController
@Api(tags = "热更新")
public class AppPatchPostController {

    @Autowired
    PatchVersionService patchVersionService;
    @Autowired
    PatchBranchService patchBranchService;
    @Autowired
    PatchConfigService patchConfigService;

    @ApiOperation(value = "发布新版本", notes = "发布新版本")
    @PostMapping("v1/app-patch/version/{os}/{branch}/release")
    public JSONResult createVersion(@ApiParam(value = "系统") @PathVariable("os") String os,
                                    @ApiParam(value = "分支", required = true) @PathVariable("branch") String branch,
                                    @Valid @RequestBody AppPatchReleaseVersionParam param){

        AppPatchConfig appPatchConfig = patchConfigService.get();
        if(!param.getSecretKey().equals(appPatchConfig.getSecretKey())){
            throw new HandleException("密钥不正确");
        }

        patchVersionService.createVersion(os, branch,
                param.getVersionMessage(), param.getVersionUrl(), param.getPatchUrl(),
                param.getBundleUrl(), param.getUpdateType(), param.getUpdateStatus());

        return JSONResult.ok();
    }

    @ApiOperation(value = "设置分支基础包", notes = "设置分支基础包")
    @PostMapping("v1/app-patch/branch/{branch}/base-package")
    public JSONResult branchBasePackage(
                                    @ApiParam(value = "分支", required = true) @PathVariable("branch") String branch,
                                    @Valid @RequestBody AppPatchBranchBasePackageParam param){

        AppPatchConfig appPatchConfig = patchConfigService.get();
        if(!param.getSecretKey().equals(appPatchConfig.getSecretKey())){
            throw new HandleException("密钥不正确");
        }

        patchBranchService.updateBasePackage(branch, param.getPackageUrl());
        return JSONResult.ok();
    }

    @ApiOperation(value = "获取分支基础包", notes = "获取分支基础包")
    @GetMapping("v1/app-patch/branch/{branch}/base-package")
    public JSONResult<PatchBranch> branchBasePackageGet(
            @ApiParam(value = "分支", required = true) @PathVariable("branch") String branch){

        PatchBranch byBranchName = patchBranchService.findByBranchName(branch);
        if(byBranchName == null){
            throw new HandleException("当前分支不存在");
        }
        return JSONResult.ok(byBranchName);
    }

}
