package com.fzm.mall.server.front.system.controller;


import cn.fzm.chain.simplesdk.util.StringUtil;
import com.alibaba.fastjson.JSONObject;
import com.fzm.mall.server.front.base.ResponseVO;
import com.fzm.mall.server.front.repo.ResponseFactory;
import com.fzm.mall.server.front.system.model.SysVersion;
import com.fzm.mall.server.front.system.service.ISysVersionService;
import com.fzm.mall.server.front.user.model.Header;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import springfox.documentation.annotations.ApiIgnore;

@Api(tags = "系统-版本更新上传接口")
@Slf4j
@RestController
@RequestMapping("/mall/system/version")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysVersionController {
    private final ISysVersionService versionService;
    private final ResponseFactory resFac;

    @ApiOperation(value = "判断是否强制更新")
    @PostMapping("/without/update")
    public ResponseVO list(@ApiIgnore @RequestAttribute Header header) {
        String version = header.getVersion();
        String platform = header.getPlatform();
        if (StringUtil.isEmpty(platform) || StringUtil.isEmpty(version)) {
            return resFac.getSimpleFailureResponse();
        }
        JSONObject data = new JSONObject();
        SysVersion sysVersion = versionService.getVersion(platform);
        if (sysVersion != null) {
            if (platform.toLowerCase().equals("ios")) {
                data.put("force", compare(sysVersion.getForceVersion(), version));
                data.put("latest", sysVersion.getVersionName());
                data.put("url", sysVersion.getAddress());
                data.put("content", sysVersion.getContent());
            } else {
                data.put("force", Integer.parseInt(sysVersion.getForceVersion()) > Integer.parseInt(version));
                data.put("latest", sysVersion.getVersionCode());
                data.put("versionName", sysVersion.getVersionName());
                data.put("url", sysVersion.getAddress());
                data.put("content", sysVersion.getContent());
            }
        }
        return resFac.getSuccessResponseWithData(data);
    }

    private boolean compare(String a, String b) {
        String[] astr = a.split("\\.");
        String[] bstr = b.split("\\.");
        for (int i = 0; i < astr.length; i++) {
            if (Integer.parseInt(astr[i]) > Integer.parseInt(bstr[i])) {
                return true;
            } else if (Integer.parseInt(astr[i]) < Integer.parseInt(bstr[i])) {
                return false;
            }
        }
        return false;
    }

    @ApiOperation(value = "获取版本信息")
    @GetMapping("/without/getSysVersion")
    public ResponseVO list() {
        JSONObject data = new JSONObject();
        SysVersion android = versionService.getVersion("android");
        SysVersion ios = versionService.getVersion("ios");
        data.put("android", android);
        data.put("ios", ios);
        return resFac.getSuccessResponseWithData(data);
    }
}
