package com.malk.mc.event;

import com.alibaba.fastjson.JSONObject;
import com.malk.delegate.TBEvent;
import com.malk.mc.service.McPocTbService;
import com.malk.utils.UtilMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Map;

@Primary
@Service
@Slf4j
public class McTbEventImpl implements TBEvent {

    @Autowired
    private McPocTbService mcPocTbService;


    @Override
    public void callBackTask(JSONObject eventJson) {
        Map data = (Map) eventJson.get("data");
        String activityId = String.valueOf(data.get("activityId"));
        Map content = (Map) data.get("content");
        String creatorId = String.valueOf(data.get("creatorId"));
        String taskId = String.valueOf(data.get("taskId"));
        String createDate = String.valueOf(data.get("created"));
        String updateDate = String.valueOf(data.get("updated"));

        mcPocTbService.saveWeekly(activityId, content, creatorId, taskId, createDate, updateDate);

    }

    @Override
    public void callBackProject(JSONObject eventJson) {

    }

    @Async
    @Override
    public void callBackWorktime(JSONObject eventJson) {
        String orgId = eventJson.getString("orgId");
        JSONObject data=eventJson.getJSONObject("data");
        if(orgId.equals("6034c885e71842e1e5bb5218")){
            // poc
            mcPocTbService.saveWorkTimes(data.getString("taskId"),data.getString("userId"), UtilMap.getList(data,"workTimeIds"));
        }
    }

    // {
    //    "eventId": "6671330be541008d302bbf11",
    //    "resourceId": "",
    //    "data": {
    //        "created": "2024-06-18T07:11:07.671Z",
    //        "dates": [
    //            "2024-06-18T00:00:00.000Z"
    //        ],
    //        "executorId": "616fb6f78ad4104a10515809",
    //        "id": "6671330b546ad300120e4c5c",
    //        "organizationId": "6034c885e71842e1e5bb5218",
    //        "taskId": "665ee80a57df029b4375c6de",
    //        "updated": "2024-06-18T07:11:07.671Z",
    //        "userId": "616fb6f78ad4104a10515809",
    //        "workTime": 7200000,
    //        "workTimeIds": [
    //            "6671330b546ad300120e4c5c"
    //        ]
    //    },
    //    "hookId": "65b37d9bfd71812b35f89aa4",
    //    "event": "worktime.create",
    //    "category": "",
    //    "orgId": "6034c885e71842e1e5bb5218",
    //    "timestamp": 1718694667768
    // }
}
