package com.jeequan.jeepay.mch.service;

import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.utils.SeqKit;
import com.jeequan.jeepay.db.entity.DeviceProvideConfig;
import com.jeequan.jeepay.db.entity.MchStoreDevice;
import com.jeequan.jeepay.service.impl.DeviceProvideConfigService;
import com.jeequan.jeepay.service.impl.MchStoreDeviceService;
import com.jeequan.jeepay.thirdparty.device.speaker.lklsSpeaker.LklsSpeakerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.image.BufferedImage;
import java.io.File;
import java.util.List;

/**
 * TODO
 * 营销相关广告设置
 * @author crystal
 * @date 2024/2/19 14:19
 */
@Service
public class MarketAdvertService {


    @Autowired private LklsSpeakerService lklsSpeakerService;
    @Autowired private MchStoreDeviceService mchStoreDeviceService;
    @Autowired private DeviceProvideConfigService deviceProvideConfigService;


    /**
     * 获取设备跑马灯广告
     * @param record
     * @return
     */
    public JSONObject getMarqueeInfo(MchStoreDevice record) {
        JSONObject deviceParams = init(record);
        return lklsSpeakerService.getMarqueeInfo(deviceParams);
    }


    /**
     * 设置跑马灯广告
     * @param record
     */
    public void saveMarqueeInfo(MchStoreDevice record) {
        record.preCheckMarqueeInfo();
        JSONObject deviceParams = init(record);
        boolean flag = mchStoreDeviceService.saveOrUpdate(record);
        if(flag){
            JSONObject bizData = new JSONObject();
            bizData.put("translsNo", SeqKit.genFlowNo("F"));
            bizData.put("devSn",record.getDeviceNo());
            bizData.put("marqueeVo",record.getMarqueeInfo());
            lklsSpeakerService.setMarqueeInfo(deviceParams,bizData);
        }
    }

    /**
     * 获取设备营销区坐标信息
     * @param record
     * @return
     */
    public JSONObject getMarketLocation(MchStoreDevice record) {
        JSONObject deviceParams = init(record);
        return lklsSpeakerService.getMarketAreaAdvert(deviceParams);
    }

    private JSONObject init(MchStoreDevice record) {
        MchStoreDevice device = mchStoreDeviceService.getDeviceInfo(record.getDeviceNo(), MchStoreDevice.DEVICE_TYPE_SPEAKER, CS.DEVICE_PROVIDER.LKLS);
        if(!record.getMchNo().equals(device.getMchNo())){
            throw new BizException("当前设备不属于你，暂不支持当前操作");
        }
        DeviceProvideConfig provideConfig = deviceProvideConfigService.getById(device.getConfigId());
        JSONObject deviceParams = new JSONObject();
        deviceParams.put("deviceParams", device.getDeviceParams());
        deviceParams.put("bizConfigParams", device.getBizConfigParams());
        deviceParams.put("providerParams", provideConfig.getProviderParams());
        deviceParams.put("device", device);
        record.setDeviceId(device.getDeviceId());
        return deviceParams;
    }

    /**
     * 获取设备营销区图片信息
     * @param record
     * @return
     */
    public JSONArray getMarketPic(MchStoreDevice record) {
        JSONObject deviceParams = init(record);
        return lklsSpeakerService.getMarketPic(deviceParams);
    }

    /**
     * 设置轮播图
     * @param record
     */
    public void saveMarketPic(MchStoreDevice record) {
        record.preCheckMarketPic();
        JSONObject deviceParams = init(record);
        boolean flag = mchStoreDeviceService.saveOrUpdate(record);
        if(flag){
            JSONObject bizData = new JSONObject();
            bizData.put("translsNo", SeqKit.genFlowNo("F"));
            bizData.put("devSn",record.getDeviceNo());
            bizData.put("markPicVos",record.getCarouselPicInfo());
            lklsSpeakerService.setMarketPic(deviceParams, bizData);
        }
    }

    /**
     * 查询设备活动码信息
     * @param record
     * @return
     */
    public JSONObject getActivityQrCode(MchStoreDevice record) {
        record.preCheckMarket();
        JSONObject deviceParams = init(record);
        JSONObject result = lklsSpeakerService.getActivityQrCode(deviceParams);
        return result;
    }

    /**
     * 获取营销设备广告详情
     * @param record
     * @return
     */
    public JSONObject getMarketAdvertInfo(MchStoreDevice record) {
        JSONObject deviceParams = init(record);
        MchStoreDevice device = deviceParams.getObject("device", MchStoreDevice.class);
        JSONObject result = new JSONObject(2);
        result.put("marqueeInfo",device.getMarqueeInfo());
        result.put("carouselPicInfo",device.getCarouselPicInfo());
        return result;
    }

    /**
     * 获取设备付款区域坐标
     * @param record
     * @return
     */
    public JSONObject getMarketPayCodeInfo(MchStoreDevice record) {
        record.preCheckMarket();
        JSONObject deviceParams = init(record);
        JSONObject result = lklsSpeakerService.getMarketPayCodeInfo(deviceParams);
        return result;
    }

    /**
     * 获取设备付款区域图片信息
     * @param record
     * @return
     */
    public JSONObject getMarketPayCodePic(MchStoreDevice record) {
        record.preCheckMarket();
        JSONObject deviceParams = init(record);
        return lklsSpeakerService.getMarketPayCodePic(deviceParams);
    }

    /**
     * 设置付款区域的图片信息
     * @param record
     */
    public void setMarketPayCodePic(MchStoreDevice record) {
        record.preCheckPayCode();
        JSONObject deviceParams = init(record);
        boolean flag = mchStoreDeviceService.saveOrUpdate(record);
        if(flag){
            JSONObject bizData = new JSONObject();
            bizData.put("translsNo", SeqKit.genFlowNo("F"));
            bizData.put("devSn",record.getDeviceNo());
//            bizData.put("payCodeVo",record.getPayPicInfo());
            lklsSpeakerService.setMarketPayCodePic(deviceParams, bizData);
        }
    }
}
