package com.softwarebr.nunu.web.api;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.brframework.commondb.core.ControllerPageHandle;
import com.brframework.commonsecurity.core.SecurityContextHolder;
import com.brframework.commonweb.json.JSONResult;
import com.brframework.commonweb.json.Page;
import com.brframework.commonweb.json.PageParam;
import com.softwarebr.nunu.domain.PlatformModular;
import com.softwarebr.nunu.dto.ser.PlatformParamDTO;
import com.softwarebr.nunu.entity.ser.Platform;
import com.softwarebr.nunu.globals.SystemConst;
import com.softwarebr.nunu.json.api.VerifyResult;
import com.softwarebr.nunu.json.api.ser.PlatformDetailsResult;
import com.softwarebr.nunu.json.api.ser.PlatformParam;
import com.softwarebr.nunu.json.api.ser.PlatformQueryParam;
import com.softwarebr.nunu.json.api.ser.PlatformResult;
import com.softwarebr.nunu.service.ser.PlatformService;
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;
import java.util.List;

import static com.brframework.commondb.core.ControllerPageHandle.convertTo;

/**
 * @date 2020-02-24 13:22:47
 * @author lilin
 */

@RestController
@Api(tags = "创新资源服务平台")
@RequestMapping("/api")
public class ApiSerController {

	@Autowired
	private PlatformService platformService;

	@ApiOperation(value = "首页->创新资源服务平台")
	@GetMapping("v1/ser/index/platform/list")
	public JSONResult<Page<PlatformResult>> indexPlatformPageV1(PageParam page) {
		PlatformQueryParam param = new PlatformQueryParam();
		param.setStatus(SystemConst.STATUS_UPPER_SHELF);
		param.setIndexType(SystemConst.INDEX_TYPE_YES);
		Page<PlatformResult> resultPage = ControllerPageHandle.jpaPageConvertToPage(platformService.page(page, param), page, PlatformResult.class);
		return JSONResult.ok(resultPage);
	}

	@ApiOperation(value = "创新资源服务平台列表")
	@GetMapping("access/v1/ser/platform/list")
	public JSONResult<Page<PlatformResult>> platformPageV1(PageParam page) {
		PlatformQueryParam param = new PlatformQueryParam();
		param.setStatus(SystemConst.STATUS_UPPER_SHELF);
		Page<PlatformResult> resultPage = ControllerPageHandle.jpaPageConvertToPage(platformService.page(page, param), page, PlatformResult.class);
		return JSONResult.ok(resultPage);
	}

	@ApiOperation(value = "创新资源服务平台列表->首页导航栏")
	@GetMapping("v1/ser/platform/banner/list")
	public JSONResult<List<PlatformResult>> platformBannerUpperV1() {
		List<PlatformResult> resultPage = ControllerPageHandle.convertListTo(platformService.getPlatformUpper(), PlatformResult.class);
		return JSONResult.ok(resultPage);
	}

	@ApiOperation(value = "创新资源服务平台详情")
	@GetMapping("access/v1/ser/platform/details/{id}")
	public JSONResult<PlatformDetailsResult> platformDetailsV1(@ApiParam("服务平台Id") @PathVariable Long id) {
		Platform platform = platformService.accessObject(id);
		PlatformDetailsResult detailsResult = ControllerPageHandle.convertTo(platform, PlatformDetailsResult.class);
		if(null != platform.getModular()){
			detailsResult.setModulars(JSONArray.parseArray(platform.getModular(), PlatformModular.class));
		}
		return JSONResult.ok(detailsResult);
	}


	@ApiOperation(value = "个人中心->平台入驻")
	@PutMapping("access/v1/ser/platform")
	public JSONResult<PlatformResult> platformCreateV1(@Valid @RequestBody PlatformParam param) {
		Long memberId = Long.valueOf(SecurityContextHolder.getSubject().getId());
		PlatformParamDTO platformParamDTO = convertTo(param, PlatformParamDTO.class);
		platformParamDTO.setModularJson(JSONObject.toJSONString(param.getModulars()));
		Platform platform = platformService.savePlatform(memberId, platformParamDTO);
		return JSONResult.ok(ControllerPageHandle.convertTo(platform, PlatformResult.class));
	}

	@ApiOperation(value = "平台入驻->认证结果")
	@GetMapping("access/v1/ser/platform/result")
	public JSONResult<VerifyResult> platformResultV1() {
		Long memberId = Long.valueOf(SecurityContextHolder.getSubject().getId());
		VerifyResult verifyResult = ControllerPageHandle.convertTo(platformService.getPlatformResult(memberId), VerifyResult.class);
		return JSONResult.ok(verifyResult);
	}

}