package com.gic.haoban.manage.web.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.gic.haoban.common.utils.HaobanResponse;
import com.gic.haoban.manage.api.dto.ApplicationDTO;
import com.gic.haoban.manage.api.dto.ApplicationSettingDTO;
import com.gic.haoban.manage.api.service.ApplicationApiService;
import com.gic.haoban.manage.api.service.ApplicationSettingApiService;
import com.gic.haoban.manage.web.errCode.HaoBanErrCode;
import com.gic.haoban.manage.web.vo.SdkVersionLimitVo;
import com.gic.redis.data.util.RedisUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class ApplicationController extends WebBaseController {

    @Autowired
    private ApplicationApiService applicationApiService;
    @Autowired
    private ApplicationSettingApiService applicationSettingApiService;


    private static final String SDKVERSION_KEY = "haoban-wx-sdk-version";


    @RequestMapping("application-list")
    public HaobanResponse applicationList(String wxEnterpriseId) {
        List<ApplicationDTO> list = applicationApiService.listApplicationByWxEnterpriseId(wxEnterpriseId);
        List<String> applicationIds = list.stream().map(s -> s.getApplicationId()).collect(Collectors.toList());
        List<ApplicationSettingDTO> settings = applicationSettingApiService.listOpenByWxEnterpriseIdAndApplicationIds(wxEnterpriseId, applicationIds);
        List<String> openApplicationIds = new ArrayList<String>();
        if (CollectionUtil.isNotEmpty(settings)) {
            openApplicationIds = settings.stream().map(s -> s.getApplicationId()).collect(Collectors.toList());
        }
        Iterator<ApplicationDTO> it = list.listIterator();
        while (it.hasNext()) {
            ApplicationDTO app = it.next();
            if (!openApplicationIds.contains(app.getApplicationId())) {
                it.remove();
                continue;
            }
            if ("通讯录".equals(app.getApplicationName())) {
                it.remove();
                continue;
            }
            if ("卡券赠送".equals(app.getApplicationName())) {
                it.remove();
                continue;
            }
            if ("内购券".equals(app.getApplicationName())) {
                it.remove();
                continue;
            }
        }

        return resultResponse(HaoBanErrCode.ERR_1, list);

    }

    @RequestMapping("sdk-version-check")
    public HaobanResponse sdkVersionCheck(String skdVersion) {
        if (StringUtils.isBlank(skdVersion)) {
            return resultResponse(HaoBanErrCode.ERR_2);
        }
        SdkVersionLimitVo sdkVersionLimitVo = new SdkVersionLimitVo();
        Object cache = RedisUtil.getCache(SDKVERSION_KEY);
        if (null == cache) {
            return resultResponse(HaoBanErrCode.ERR_1, sdkVersionLimitVo);
        }
        String limitSdkVersion = cache.toString();
        sdkVersionLimitVo.setLimitSdkVersion(limitSdkVersion);

        String[] limitSdk = StringUtils.split(limitSdkVersion, ".");
        String[] versionSplit = StringUtils.split(skdVersion, ".");

        int checkFlag = checkNum(Integer.valueOf(limitSdk[0]), Integer.valueOf(versionSplit[0]));
        if (checkFlag != 0) {
            sdkVersionLimitVo.setCheckFlag(checkFlag > 0 ? true : false);
            return resultResponse(HaoBanErrCode.ERR_1, sdkVersionLimitVo);
        }
        checkFlag = checkNum(Integer.valueOf(limitSdk[1]), Integer.valueOf(versionSplit[1]));
        if (checkFlag != 0) {
            sdkVersionLimitVo.setCheckFlag(checkFlag > 0 ? true : false);
            return resultResponse(HaoBanErrCode.ERR_1, sdkVersionLimitVo);
        }
        checkFlag = checkNum(Integer.valueOf(limitSdk[2]), Integer.valueOf(versionSplit[2]));
        if (checkFlag != 0) {
            sdkVersionLimitVo.setCheckFlag(checkFlag > 0 ? true : false);
            return resultResponse(HaoBanErrCode.ERR_1, sdkVersionLimitVo);
        }
        return resultResponse(HaoBanErrCode.ERR_1, sdkVersionLimitVo);

    }

    private int checkNum(int limit, int cur) {
        return cur > limit ? 1 : (cur == limit ? 0 : -1);
    }

}
