package com.om.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.om.entity.dto.*;
import com.om.entity.po.Client;
import com.om.entity.po.Manual;
import com.om.entity.po.Wxsc;
import com.om.entity.vo.AppManual;
import com.om.exception.BadReqException;
import com.om.exception.BizException;
import com.om.mapper.ManualMapper;
import com.om.service.IClientService;
import com.om.service.IManualService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.om.utils.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author henry-ong
 * @since 2024-02-28
 */
@Service
public class ManualServiceImpl extends ServiceImpl<ManualMapper, Manual> implements IManualService {
  private final ManualMapper manualMapper;

  public ManualServiceImpl(ManualMapper manualMapper) {
    this.manualMapper = manualMapper;
  }

  @Resource
  private JwtUtils jwtUtils;

  @Resource
  private IClientService clientService;

  @Resource
  private PushUtil pushUtil;

  @Override
  public Page<Manual> findManual(Page<Manual> page, String code, String title) {
    return manualMapper.findManual(page, code, title);
  }

    @Override
    public Result<AppManual> AppGetByLang(AppBaseDTO dto) {
        String language = dto.getLanguage();
        String guid = dto.getGuid();
        GuidDTO guidDTO = AesUtil.getGuidDTOFromGuid(guid);
        String clientNum = guidDTO.getClientNum();
        Manual one = this.lambdaQuery()
                .eq(Manual::getLang, language)
                .eq(Manual::getClientCode,clientNum)
                .one();

        if (BeanUtil.isEmpty(one)){
            throw new BadReqException("该使用手册不存在");
        }
        AppManual vo = new AppManual();
        BeanUtil.copyProperties(one, vo);
        vo.setCreateTime(one.getCreateTime().toInstant(ZoneOffset.of("+8")).toEpochMilli());
        return Result.ok(vo);
    }

    @Override
    public Result pushManual(Integer id) {
        Manual manual = this.getById(id);
        if (BeanUtil.isEmpty(manual)){
            throw new BadReqException("该使用手册不存在");
        }
        PushInfo pushInfo = new PushInfo();
        pushInfo.setPushInfoType(4);

        MaintenanceManualInfo maintenanceManualInfo = new MaintenanceManualInfo();
        maintenanceManualInfo.setType("1");
        List<Describe> describeList = new ArrayList<>();
        String title = manual.getTitle();
        List<Manual> manuals = this.lambdaQuery()
                .eq(Manual::getTitle, title)
                .eq(Manual::getClientCode,manual.getClientCode())
                .list();
        if (CollectionUtil.isNotEmpty(manuals)){
            for (Manual m : manuals) {
                Describe describe = new Describe();
                describe.setLanguage(m.getLang());
                describe.setDescription(m.getFileName());
                describeList.add(describe);
            }
        }

        maintenanceManualInfo.setDescribeList(describeList);
        pushInfo.setJson(GsonUtils.toJsonString(maintenanceManualInfo));

        String clientCode = manual.getClientCode();
        if (StringUtils.isNotBlank(clientCode)){

            Client client = clientService.lambdaQuery()
                    .eq(Client::getNumber, clientCode)
                    .one();

            if (BeanUtil.isEmpty(client)){
                throw new BadReqException("该客户端编号不存在");
            }
            String deviceToken = client.getDeviceToken();
            if (StringUtils.isNotBlank(deviceToken)){
                String[] token = new String[]{deviceToken};
                try {
                    pushUtil.sendAndroidListcastJSON(token,pushInfo);
                } catch (Exception e) {
                    throw new BizException("推送失败");
                }
            }
        }
        return Result.ok();
    }
}
