package com.casic.PgInterface.rs;

import com.casic.PgInterface.devTable.domain.PgDevice;
import com.casic.PgInterface.devTable.manager.PgDeviceManager;
import com.casic.PgInterface.overtimeInPG.domain.PGTagMac;
import com.casic.PgInterface.overtimeInPG.domain.PGTagPosition;
import com.casic.PgInterface.overtimeInPG.dto.PGPositionDto;
import com.casic.PgInterface.overtimeInPG.dto.PGTagPositionDto;
import com.casic.PgInterface.overtimeInPG.manager.PGInOutRecManager;
import com.casic.PgInterface.overtimeInPG.manager.PGTagMacManager;
import com.casic.PgInterface.overtimeInPG.manager.PGTagPositionManager;
import com.casic.PgInterface.overtimeInPG.task.TagmacRun;
import com.casic.PgInterface.patroler.domain.PgInPgPerson;
import com.casic.PgInterface.patroler.domain.PgInPgStatus;
import com.casic.PgInterface.patroler.manager.PgInPgPersonManager;
import com.casic.PgInterface.patroler.manager.PgInPgStatusManager;
import net.sf.json.JSONObject;
//import org.apache.commons.beanutils.BeanMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.stereotype.Component;
//import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component
@Path("InOutPGrec")
public class PGInOutRecRs {

    @Resource
    private PGInOutRecManager pgInOutRecManager;
    @Resource
    private PGTagPositionManager pgTagPositionManager;
    @Resource
    private PgInPgPersonManager pgInPgPersonManager;
    @Resource
    private PGTagMacManager pgTagMacManager;
    @Resource
    private PgInPgStatusManager pgInPgStatusManager;
    @Resource
    private TagmacRun tagmacRun;
    @Resource
    private PgDeviceManager pgDeviceManager;

    private static Logger logger = LoggerFactory.getLogger(PGInOutRecRs.class);

    @POST
    @Path("query")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String , Object> query(@FormParam("dtFrom") String dtFrom,
                                      @FormParam("dtTo") String dtTo)
    {
        Map<String,Object> resultMap = new HashMap<>();
        resultMap = pgInOutRecManager.getInOutRec(dtFrom,dtTo);
        return resultMap;
    }

    @POST
    @Path("getRecentPosition")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String,Object> getRecentPosition(@FormParam("inPgPersonId") String inPgPersonId)
    {
        logger.info("param:"+inPgPersonId);
        Map<String, Object> resultMap = new HashMap<>();
        PgInPgPerson pgInPgPerson = pgInPgPersonManager.get(Long.valueOf(inPgPersonId));
        PGTagMac pgTagMac = pgTagMacManager.findUniqueBy("hostName",pgInPgPerson.getAssetCode());
//        PGTagMac pgTagMac = pgTagMacManager.findUniqueBy("tagMac",pgInPgPerson.getAssetCode());
//        long maxId = pgTagPositionManager.getMaxId(pgTagMac.getTagMac());
//        PGTagPosition pgTagPosition = pgTagPositionManager.get(maxId);
        PGTagPosition pgTagPosition = tagmacRun.GetTagStatus(pgTagMac);
        boolean bExist = false;
        bExist = pgTagPositionManager.findTagPosition(pgTagPosition.getPositionUpdatetime(),pgTagPosition.getHostId());
        if (!bExist)
            pgTagPositionManager.save(pgTagPosition);

        String coorName = pgTagPosition.getCoordinatesName();
        System.out.println("position:"+coorName.substring(0,coorName.indexOf("号分区")));
        PgDevice pgDevice = pgDeviceManager.findUniqueBy("pipeGallery",coorName.substring(0,coorName.indexOf("号分区")));

        Map<Object,Object> resMap = new HashMap<>();
        JSONObject jsonObject = JSONObject.fromObject(new PGPositionDto(pgTagPosition));
        resMap.putAll(jsonObject);
        resMap.put("partition",pgDevice == null ? coorName : pgDevice.getPartition());
        resMap.put("binName",pgDevice == null ? coorName:coorName.substring(coorName.indexOf("号分区")+3,coorName.indexOf("K")));

        resultMap.put("msg","success");
        resultMap.put("result",resMap);
        return resultMap;
    }

    @POST
    @Path("getHistoryByTagmac")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String,Object> getHistoryByTagmac(@FormParam("inPgPersonId") String inPgPersonId)
    {
        logger.info("param:"+inPgPersonId);
        Map<String, Object> resultMap = new HashMap<>();
        PgInPgPerson pgInPgPerson = pgInPgPersonManager.get(Long.valueOf(inPgPersonId));
        PgInPgStatus pgInPgStatus = pgInPgStatusManager.findUniqueBy("pgInPgPersonId",pgInPgPerson);
        System.out.println("time:"+pgInPgStatus.getInTime());
//        PgInPgStatus pgInPgStatus = pgInPgStatusManager.findUniqueBy("pgInPgPersonId",inPgPersonId);
//        PGTagMac pgTagMac = pgTagMacManager.findUniqueBy("tagMac",pgInPgPerson.getAssetCode());
        PGTagMac pgTagMac = pgTagMacManager.findUniqueBy("hostName",pgInPgPerson.getAssetCode());

        Map<Object,Object> mapPosition = new HashMap<>();
        if(pgInPgStatus.getOutTime() == null)
            mapPosition = pgTagPositionManager.getHisByHostIdView(pgInPgStatus.getInTime(),new Date(),pgTagMac.getHostId());
        else
            mapPosition = pgTagPositionManager.getHisByHostIdView(pgInPgStatus.getInTime(),pgInPgStatus.getOutTime(),pgTagMac.getHostId());

        JSONObject jsonObject = JSONObject.fromObject(pgTagMac);
        jsonObject.remove("id");
        mapPosition.putAll(jsonObject);
        resultMap.put("msg","success");
        resultMap.put("result",mapPosition);
        return resultMap;
    }



}