package com.mortals.xhx.module.skin.web;

import com.alibaba.fastjson.JSON;
import com.mortals.framework.annotation.UnAuth;
import com.mortals.framework.common.Rest;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.common.pdu.device.DeviceMsgReqPdu;
import com.mortals.xhx.feign.device.IDeviceMessageFeign;
import com.mortals.xhx.module.skin.model.SkinEntity;
import com.mortals.xhx.module.skin.service.SkinService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 皮肤
 *
 * @author zxfei
 * @date 2023-04-03
 */
@RestController
@RequestMapping("skin")
@Slf4j
public class SkinController extends BaseCRUDJsonBodyMappingController<SkinService, SkinEntity, Long> {

    @Autowired
    private ParamService paramService;

    @Autowired
    private IDeviceMessageFeign deviceMessageFeign;

    public SkinController() {
        super.setModuleDesc("皮肤");
    }

    @Override
    protected void init(Map<String, Object> model, Context context) {
        this.addDict(model, "imageResolution", paramService.getParamBySecondOrganize("Skin", "imageResolution"));
        this.addDict(model, "used", paramService.getParamBySecondOrganize("Skin", "used"));
        super.init(model, context);
    }

    @Override
    protected int saveAfter(SkinEntity entity, Map<String, Object> model, Context context) throws AppException {

        //推送皮肤更新消息
        DeviceMsgReqPdu deviceMsgReqPdu = new DeviceMsgReqPdu();
        deviceMsgReqPdu.setSiteid(entity.getSiteId());
        deviceMsgReqPdu.setProductCode("ybj");
        deviceMsgReqPdu.setMessageType("edition");
        deviceMsgReqPdu.setData("W10=");
        deviceMsgReqPdu.setTimestamp(System.currentTimeMillis());
        Rest<String> rest = deviceMessageFeign.callbackByProduct(deviceMsgReqPdu);
        log.info("send device message==>{}", JSON.toJSONString(rest));
        return super.saveAfter(entity, model, context);
    }

    /**
     *皮肤使用
     */
    @PostMapping(value = "used")
    public Rest<String> used(@RequestBody SkinEntity entity) {
        String busiDesc = this.getModuleDesc() + "皮肤选择使用";
        Rest<String> rest = Rest.ok(busiDesc + " 【成功】");
        try {
            if (ObjectUtils.isEmpty(entity.getId())) {
                throw new AppException("皮肤Id不能为空！");
            }
            SkinEntity skinEntity = this.service.get(entity.getId(), getContext());
            if (ObjectUtils.isEmpty(skinEntity)) {
                throw new AppException("当前皮肤不存在！skinId:" + entity.getId());
            }
            if(skinEntity.getUsed()==entity.getUsed()&&skinEntity.getUsed()==YesNoEnum.YES.getValue()){
                throw new AppException("当前皮肤不能取消选择！" );
            }
            if (skinEntity.getUsed() == YesNoEnum.YES.getValue()) {
                throw new AppException("当前皮肤使用中！");
            }
            Rest<Void> usedRest = this.service.appUsed(skinEntity, getContext());
            recordSysLog(request, busiDesc + " 【成功】");
        } catch (Exception e) {
            log.error("皮肤切换", e);
            return Rest.fail(super.convertException(e));
        }
        return rest;
    }


    @PostMapping({"refreshSkin"})
    @UnAuth
    public Rest<Object> refreshSkin() {
        log.info("刷新皮肤");
        this.service.syncSkin();
        return Rest.ok("皮肤刷新操作成功");
    }
}