package org.springblade.modules.nh.factory.goodwe.scheduler;

import lombok.extern.slf4j.Slf4j;
import org.springblade.common.constant.NhConstants;
import org.springblade.common.utils.CommonUtil;
import org.springblade.modules.nh.factory.common.PageAction;
import org.springblade.modules.nh.factory.common.PageInfo;
import org.springblade.modules.nh.factory.ginlong.dto.inverterdetaillist.GetInverterDetailR;
import org.springblade.modules.nh.factory.goodwe.GoodweApi;
import org.springblade.modules.nh.factory.goodwe.GoodweRet;
import org.springblade.modules.nh.factory.goodwe.dto.queryinventers.QueryInventersData;
import org.springblade.modules.nh.factory.goodwe.dto.queryinventers.QueryInventersR;
import org.springblade.modules.nh.factory.goodwe.dto.queryinventers.QueryInventersReq;
import org.springblade.modules.nh.service.IMonitorInverterDailyStatService;
import org.springblade.modules.nh.service.IMonitorInverterStatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.util.*;

@Service
@Slf4j
public class GoodweInverterDetailListPageAction extends PageAction<QueryInventersData, QueryInventersR> {

    @Autowired
    GoodweApi goodweApi;
    @Autowired
	IMonitorInverterStatService monitorInverterStatService;
    @Autowired
	IMonitorInverterDailyStatService monitorInverterDailyStatService;

    @Override
    public PageInfo<QueryInventersData, QueryInventersR> pageData(int page, int pageSize, Map<String, Object> ctx) throws IOException {
        QueryInventersReq req = new QueryInventersReq();
        req.setPageIndex(page);
        req.setPageSize(pageSize);
        req.setPwId(getStationId(ctx));
		req.setIndex(CommonUtil.getVendorIndex(ctx));
        GoodweRet<QueryInventersData> resp = goodweApi.queryInventers(req);
        if (isOk(resp.getCode())) {
            PageInfo<QueryInventersData, QueryInventersR> ret = new PageInfo<>();
            QueryInventersData data = resp.getData();
            int pages = calcPage(data.getRecord(), pageSize);
            ret.setPages(pages);
            ret.setData(data);
            ret.setList(data.getList());
            return ret;
        }
        log.error("goodwe inverter detail error: {}, code: {}", resp.getMsg(), resp.getCode());
        return null;
    }

    private String getStationId(Map<String, Object> ctx) {
        return (String) ctx.get("stationId");
    }

    @Override
    public void handlePageInfo(PageInfo<QueryInventersData, QueryInventersR> pageInfo, Map<String, Object> ctx) {
        List<QueryInventersR> list = pageInfo.getList();
        List<GetInverterDetailR> convList = conv(CommonUtil.getVendorIndex(ctx),list);
        if (!CollectionUtils.isEmpty(list)) {
            try {
                Set<String> updateFields = new HashSet<>();
                updateFields.add("name");
                updateFields.add("sn");
                updateFields.add("power");
                monitorInverterStatService.syncInverterDetails(convList, NhConstants.SYS_TYPE_GOODWE, updateFields);
            } catch (Exception ex) {
                log.error("sync inverter details error", ex);
            }
        }
    }

    private List<GetInverterDetailR> conv(int index,List<QueryInventersR> list) {
        List<GetInverterDetailR> ret = new ArrayList<>();
        for (QueryInventersR queryInventersR : list) {
            ret.add(convR(index,queryInventersR));
        }
        return ret;
    }

    private GetInverterDetailR convR(int index,QueryInventersR r) {
        GetInverterDetailR ret = new GetInverterDetailR();
		ret.setVendorIndex(index);
        ret.setDataTimestamp(new Date().getTime());
        ret.setStationId(r.getPwId());
        ret.setId(r.getId());
        ret.setName(r.getItName());
        ret.setSn(r.getItSn());
        ret.setPower(r.getItCapacity());
        ret.setPowerStr("kw");
        return ret;
    }
}
