package com.caliverse.admin.scheduler.polling.service;

import com.caliverse.admin.domain.service.UserGameSessionService;
import com.caliverse.admin.dynamodb.service.DynamodbService;
import com.caliverse.admin.scheduler.CommonScheduler;
import com.caliverse.admin.scheduler.entity.SchedulerType;
import com.caliverse.admin.domain.entity.LandOwnerChange;
import com.caliverse.admin.domain.request.LandRequest;
import com.caliverse.admin.domain.service.LandService;
import com.caliverse.admin.dynamodb.service.DynamodbLandService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class LandOwnerChangesScheduler extends CommonScheduler {

    private final LandService landService;
    private final DynamodbLandService dynamodbLandService;
    private final DynamodbService dynamodbService;
    private final UserGameSessionService userGameSessionService;

    public LandOwnerChangesScheduler(LandService landService, DynamodbLandService dynamodbLandService, DynamodbService dynamodbService, UserGameSessionService userGameSessionService) {
        this.landService = landService;
        this.dynamodbLandService = dynamodbLandService;
        this.dynamodbService = dynamodbService;
        this.userGameSessionService = userGameSessionService;
    }

    @Override
    protected void executeInternal() {
        List<LandOwnerChange> scheduleList = landService.getScheduleLandOwnerChangesList();

        scheduleList.forEach(landOwnerChange -> {
            LandOwnerChange.CHANGE_STATUS status = landOwnerChange.getStatus();
            LocalDateTime reserve_dt = landOwnerChange.getReservationDt();
            int landId = landOwnerChange.getLandId();
            String guid = landOwnerChange.getUserGuid();

            LocalDateTime now = LocalDateTime.now();

            if(status.equals(LandOwnerChange.CHANGE_STATUS.WAIT) && reserve_dt.isBefore(now)) {
                LandRequest landRequest = LandRequest.builder()
                        .landId(landId)
                        .landName(landOwnerChange.getLandName())
                        .buildingId(landOwnerChange.getBuildingId())
                        .buildingName(landOwnerChange.getBuildingName())
                        .userGuid(guid)
                        .userName(landOwnerChange.getUserName())
                        .build();

                //유저킥
                userGameSessionService.kickUserSession(guid);

                //랜드정보 처리
                dynamodbLandService.ChangesLandOwner(landRequest);

                Map map = new HashMap<>();
                map.put("id", landOwnerChange.getId());
                map.put("status", LandOwnerChange.CHANGE_STATUS.FINISH);
                landService.updateLandOwnedChangeStatus(map);

                //우편
                dynamodbService.insertLandChangesMail(landRequest);
            }
        });
    }

    @Override
    public SchedulerType getSchedulerType() {
        return SchedulerType.LAND_OWNER_CHANGES;
    }
}
