package ink.wgink.login.app.service.appversion.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.exceptions.SearchException;
import ink.wgink.login.app.dao.appversion.IAppVersionDao;
import ink.wgink.login.app.pojo.dtos.appversion.AppVersionDTO;
import ink.wgink.login.app.pojo.pos.appversion.AppVersionPO;
import ink.wgink.login.app.pojo.vos.appversion.AppVersionSaveVO;
import ink.wgink.login.app.pojo.vos.appversion.AppVersionVO;
import ink.wgink.login.app.service.appversion.IAppVersionService;
import ink.wgink.module.file.service.IFileService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.util.QRCodeUtil;
import ink.wgink.util.UUIDUtil;
import ink.wgink.util.map.HashMapUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: AppDeviceServiceImpl
 * @Description: app设备
 * @Author: wanggeng
 * @Date: 2021/4/7 4:20 下午
 * @Version: 1.0
 */
@Service
public class AppVersionServiceImpl extends DefaultBaseService implements IAppVersionService {

    @Autowired
    private IAppVersionDao appVersionDao;
    @Autowired
    private IFileService fileService;
    @Value("${server.url}")
    private String serverUrl;

    @Override
    public void save(AppVersionSaveVO appVersionSaveVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(appVersionSaveVO);
        params.put("appVersion", "1");
        params.put("appDownloadCount", "0");
        params.put("appVersionId", UUIDUtil.getUUID());
        setSaveInfo(params);
        appVersionDao.save(params);
    }

    @Override
    public void remove(List<String> ids) {
        Map<String, Object> params = getHashMap(2);
        params.put("appVersionIds", ids);
        setUpdateInfo(params);
        appVersionDao.remove(params);
    }

    @Override
    public void update(String appVersionId, AppVersionVO appVersionVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(appVersionVO);
        params.put("appVersionId", appVersionId);
        setUpdateInfo(params);
        appVersionDao.update(params);
    }

    @Override
    public void updateApiLock(String appVersionId, Integer appApiLock) {
        Map<String, Object> params = getHashMap(8);
        params.put("appApiLock", appApiLock);
        params.put("appVersionId", appVersionId);
        setUpdateInfo(params);
        appVersionDao.updateApiLock(params);
    }

    @Override
    public void updateRelease(String appVersionId, int isRelease) {
        Map<String, Object> params = getHashMap(8);
        params.put("isRelease", isRelease);
        params.put("appVersionId", appVersionId);
        setUpdateInfo(params);
        appVersionDao.updateRelease(params);
    }

    @Override
    public AppVersionDTO get(Map<String, Object> params) {
        return appVersionDao.get(params);
    }

    @Override
    public AppVersionDTO get(String appVersionId) {
        Map<String, Object> params = getHashMap(2);
        params.put("appVersionId", appVersionId);
        return get(params);
    }

    @Override
    public AppVersionPO getPO(Map<String, Object> params) {
        return appVersionDao.getPO(params);
    }

    @Override
    public AppVersionPO getPO(String appVersionId) {
        Map<String, Object> params = getHashMap(2);
        params.put("appVersionId", appVersionId);
        return getPO(params);
    }

    @Override
    public AppVersionPO getPO(String appVersionId, int isRelease) {
        Map<String, Object> params = getHashMap(2);
        params.put("appVersionId", appVersionId);
        params.put("isRelease", isRelease);
        return getPO(params);
    }

    @Override
    public List<AppVersionDTO> list(Map<String, Object> params) {
        return appVersionDao.list(params);
    }

    @Override
    public SuccessResultList<List<AppVersionDTO>> listPage(ListPage page) {
        PageHelper.startPage(page.getPage(), page.getRows());
        List<AppVersionDTO> appVersionDTOs = list(page.getParams());
        PageInfo<AppVersionDTO> pageInfo = new PageInfo<>(appVersionDTOs);
        return new SuccessResultList<>(appVersionDTOs, pageInfo.getPageNum(), pageInfo.getTotal());
    }

    @Override
    public void downloadQrCode(String appVersionId, HttpServletResponse response) throws IOException {
        String qrContent = String.format("%s/app/appversion/download/%s", serverUrl, appVersionId);
        response.setContentType("image/png");
        BufferedImage bufferedImage = QRCodeUtil.createQrCode(300, 300, qrContent, null);
        ImageIO.write(bufferedImage, "png", response.getOutputStream());
    }

    @Override
    public Integer getNumber(String appVersionId) throws SearchException {
        AppVersionPO appVersionPO = getPO(appVersionId, 1);
        if (appVersionPO == null) {
            throw new SearchException("app不存在或未发布");
        }
        return appVersionPO.getAppVersion();
    }

    @Override
    public void download(String appVersionId, HttpServletRequest request, HttpServletResponse response) {
        LOG.debug("获取当前APP");
        AppVersionPO appVersionPO = getPO(appVersionId, 1);
        if (appVersionPO == null) {
            throw new SearchException("app不存在或未发布");
        }
        LOG.debug("下载APP");
        Map<String, Object> params = getHashMap(4);
        params.put("fileId", appVersionPO.getAppFile());
        params.put("isOpen", false);
        boolean isDownloadComplete = fileService.downLoadFile(request, response, params, true);
        if (isDownloadComplete) {
            LOG.debug("更新下载次数");
            appVersionDao.updateDownloadCount(appVersionPO.getAppVersionId());
        }
    }

    @Override
    public boolean canApiLock(String appVersionId, String appVersion) {
        AppVersionPO appVersionPO = getPO(appVersionId);
        if (Objects.isNull(appVersionPO)) {
            throw new SearchException("app不存在");
        }
        if (appVersionPO.getAppApiLock() == 0) {
            return false;
        }
        if (StringUtils.isBlank(appVersion)) {
            throw new SearchException("app版本号不能为空");
        }
        if (Integer.parseInt(appVersion) < appVersionPO.getAppVersion()) {
            return true;
        }
        return false;
    }

}
