package com.neusoft.smart.pos.ota.controller;


import com.neusoft.smart.pos.dto.ota.request.ApkStrategyQueryRequest;
import com.neusoft.smart.pos.dto.ota.request.ApkVersionRequest;
import com.neusoft.smart.pos.dto.ota.request.AppInfoRequest;
import com.neusoft.smart.pos.dto.ota.request.AppQueryRequest;
import com.neusoft.smart.pos.dto.ota.request.AppStrategyGroupRequest;
import com.neusoft.smart.pos.dto.ota.response.ApkStrategyGroupResponse;
import com.neusoft.smart.pos.dto.ota.response.ApkStrategyResponse;
import com.neusoft.smart.pos.dto.ota.response.ApkVersionResponse;
import com.neusoft.smart.pos.dto.ota.response.AppInfoResponse;
import com.neusoft.smart.pos.framework.bean.PaginationExtendBean;
import com.neusoft.smart.pos.framework.dto.ResponseData;
import com.neusoft.smart.pos.ota.service.AppService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@RestController
@Api(value = "App和APK管理restful接口", description = "App和APK管理restful接口")
public class AppController {
    
    @Autowired
    private AppService appService;
    
    
    @PostMapping("/apps")
    @ApiOperation(value = "分页查询app接口", notes = "分页查询app接口")
    public ResponseData<PaginationExtendBean<AppInfoResponse>> groupList(@RequestBody AppQueryRequest appQueryRequest) {
        PaginationExtendBean<AppInfoResponse> paginationExtendBean = new PaginationExtendBean<>();
        paginationExtendBean.setRows(appService.getList(appQueryRequest));
        paginationExtendBean.setTotal(appService.getCount(appQueryRequest));
        return new ResponseData<>(paginationExtendBean);
    }
    
    @PostMapping("/app")
    @ApiOperation(value = "创建或者修改app接口", notes = "创建或者修改app接口，区别为有id是修改，没有是创建")
    public ResponseData<?> createOrUpdateApp(@Valid @RequestBody AppInfoRequest appInfoRequest, BindingResult bindingResult) {
        appService.createOrUpdateApp(appInfoRequest);
        return new ResponseData();
    }
    
    @PutMapping("/app/status")
    @ApiOperation(value = "修改app状态接口", notes = "修改app状态接口")
    public ResponseData<?> changeStatus(@RequestBody AppInfoRequest appInfoRequest) {
        appService.changeStatus(appInfoRequest.getId(), appInfoRequest.getStatus());
        return new ResponseData();
    }
    
    
    @PostMapping("/apkVersion")
    @ApiOperation(value = "创建apk版接口", notes = "创建apk版本接口")
    public ResponseData<?> createApk(@Valid @RequestBody ApkVersionRequest apkVersionRequest) {
        appService.createApk(apkVersionRequest);
        return new ResponseData();
    }
    
    
    @GetMapping("/apkVersions/{appId}")
    @ApiOperation(value = "通过appId 获取所有有效apk文件接口", notes = "通过appId 获取所有有效apk文件接口")
    public ResponseData<List<ApkVersionResponse>> getApkVersionList(@PathVariable Integer appId) {
        return new ResponseData<>(appService.getApkVersionList(appId));
    }
    
    
    @PostMapping("/apkVersionStrategies")
    @ApiOperation(value = "分页查询app更新策略接口", notes = "分页查询app更新策略接口")
    public ResponseData<PaginationExtendBean<ApkStrategyGroupResponse>> getApkVersionStrategies(@RequestBody ApkStrategyQueryRequest apkStrategyQueryRequest) {
        PaginationExtendBean<ApkStrategyGroupResponse> paginationExtendBean = new PaginationExtendBean<>();
        paginationExtendBean.setRows(appService.getApkStrategyGroupList(apkStrategyQueryRequest));
        paginationExtendBean.setTotal(appService.getApkStrategyGroupCount(apkStrategyQueryRequest));
        return new ResponseData<>(paginationExtendBean);
    }
    
    @PostMapping("/apkVersionStrategy")
    @ApiOperation(value = "创建app更新策略接口", notes = "创建app更新策略接口")
    public ResponseData<?> save(@Valid @RequestBody AppStrategyGroupRequest appStrategyGroupRequest) {
        appService.saveGroup(appStrategyGroupRequest);
        return new ResponseData();
    }
    
    @GetMapping("/apkVersionStrategy/app/{groupId}")
    @ApiOperation(value = "app更新策略中查看app列表接口", notes = "app更新策略中查看app列表接口")
    public ResponseData<List<AppInfoResponse>> getApps(@PathVariable Integer groupId) {
        return new ResponseData<>(appService.getApps(groupId));
    }
    
    /**
     * 查看策略设备
     *
     * @param groupId
     * @return
     */
    @GetMapping("/apkVersionStrategy/strategies/{groupId}")
    @ApiOperation(value = "app更新策略中查看app和设备列表接口", notes = "app更新策略中查看app和设备列表接口")
    public ResponseData<List<ApkStrategyResponse>> getDevice(@PathVariable Integer groupId) {
        return new ResponseData<>(appService.getApkStrategyList(groupId));
    }
}
