package com.xiang.xservice.application.script.fwd.server;

import com.xiang.xservice.application.script.fwd.mapper.FwdUserConfigMapper;
import com.xiang.xservice.basic.common.resp.Result;
import com.xiang.xservice.basic.utils.DateUtils;
import com.xiang.xservice.application.script.fwd.entity.pojo.FPerformConfig;
import com.xiang.xservice.application.script.fwd.entity.pojo.FPerformProjectInfo;
import com.xiang.xservice.application.script.fwd.entity.pojo.FPerformSeatInfo;
import com.xiang.xservice.application.script.fwd.entity.resp.http.perform.*;
import com.xiang.xservice.application.script.fwd.service.IPerformService;
import com.xiang.xservice.application.script.fwd.service.IPerformServiceHttp;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/system/fwd")
@RequiredArgsConstructor
public class FwdDataController {

    private final IPerformService performService;
    private final IPerformServiceHttp performServiceHttp;
    private final FwdUserConfigMapper userConfigMapper;

    @PostMapping("/refreshData")
    public Result<String> refreshData() {
        String token = userConfigMapper.getAvailableUser().get(0).getToken();
        List<FPerformConfig> availablePerforms = performService.getAvailablePerform();
        for (FPerformConfig availablePerform : availablePerforms) {
            ProjectInfoResp info = performServiceHttp.getProjectInfoFromHttp(availablePerform.getProjectId(), token);
            Perform performsByProjectIdFromHttp = performServiceHttp.getPerformsByProjectIdFromHttp(availablePerform.getProjectId(), token);
            FPerformProjectInfo performByProjectId = performService.getPerformByProjectId(availablePerform.getProjectId());
            performByProjectId.setStatus(info.getStatus());
            performByProjectId.setPreSaleTime(DateUtils.getDateTimeFromStr(info.getPreSaleTime()));
            performService.updateProjectInfo(performByProjectId);
            List<PerformInfo> performInfos = performsByProjectIdFromHttp.getPerformInfos();
            if (CollectionUtils.isEmpty(performInfos)) {
                continue;
            }
            for (PerformInfo performInfo : performInfos) {
                List<PerformDetail> performInfo1 = performInfo.getPerformInfo();
                if (CollectionUtils.isEmpty(performInfo1)) {
                    continue;
                }
                for (PerformDetail performDetail : performInfo1) {
                    List<SeatPlan> seatPlans = performDetail.getSeatPlans();
                    if (CollectionUtils.isEmpty(seatPlans)) {
                        continue;
                    }
                    List<Long> seatIds = seatPlans.stream().map(SeatPlan::getSeatPlanId).collect(Collectors.toList());
                    List<SeatPlanStatus> seatPlanStatusFromHttp = performServiceHttp.getSeatPlanStatusFromHttp(seatIds, token);
                    if (CollectionUtils.isEmpty(seatPlanStatusFromHttp)) {
                        continue;
                    }
                    Map<Long, SeatPlanStatus> map = seatPlanStatusFromHttp.stream().collect(Collectors.toMap(SeatPlanStatus::getSeatPlanId, Function.identity(), (a, b) -> a));
                    for (SeatPlan seatPlan : seatPlans) {
                        FPerformSeatInfo seatInfoBySeatId = performService.getSeatInfoBySeatId(seatPlan.getSeatPlanId());
                        if (Objects.isNull(seatInfoBySeatId)) {
                            FPerformSeatInfo fPerformSeatInfo = new FPerformSeatInfo();
                            fPerformSeatInfo.setSeatPlanId(seatPlan.getSeatPlanId());
                            fPerformSeatInfo.setSeatPlanName(seatPlan.getSeatPlanName());
                            fPerformSeatInfo.setPerformId(seatPlan.getPerformId());
                            fPerformSeatInfo.setPerformName(seatPlan.getPerformName());
                            fPerformSeatInfo.setStopSale(seatPlan.getStopSale());
                            fPerformSeatInfo.setShelfStatus(seatPlan.getShelfStatus());
                            fPerformSeatInfo.setPrice(seatPlan.getPrice());
                            fPerformSeatInfo.setDiscountPrice(seatPlan.getDiscountPrice());
                            fPerformSeatInfo.setSubStatus(seatPlan.getSubStatus());
                            fPerformSeatInfo.setQuantity(seatPlan.getQuantity());
                            fPerformSeatInfo.setStatus(seatPlan.getStatus());
                            fPerformSeatInfo.setMaxSellStock(seatPlan.getMaxSellStock());
                            fPerformSeatInfo.setSoldStock(seatPlan.getSoldStock());
                            fPerformSeatInfo.setLeftStock(seatPlan.getLeftStock());
                            fPerformSeatInfo.setAbleSaleQuantity(seatPlan.getAbleSaleQuantity());
                            fPerformSeatInfo.setAshShow(seatPlan.getAshShow());
                            fPerformSeatInfo.setAshShowDesc(seatPlan.getAshShowDesc());
                            fPerformSeatInfo.setSelectable(seatPlan.getSelectable());
                            fPerformSeatInfo.setDisplay(seatPlan.getDisplay());
                            fPerformSeatInfo.setAvailableTicketQuantity(seatPlan.getAvailableTicketQuantity());
                            fPerformSeatInfo.setAvailableAllTicketQuantity(seatPlan.getAvailableAllTicketQuantity());
                            fPerformSeatInfo.setSaleTime(DateUtils.getDateTimeFromStr(seatPlan.getSaleTime(), "yyyy/MM/dd HH:mm:ss"));
                            fPerformSeatInfo.setProjectId(availablePerform.getProjectId());
                            fPerformSeatInfo.setSoldOut(seatPlan.getSoldStock());
                            performService.addSeatInfo(fPerformSeatInfo);
                        } else {
                            SeatPlanStatus seatPlanStatus = map.get(seatPlan.getSeatPlanId());
                            if (Objects.isNull(seatPlanStatus)) {
                                continue;
                            }
                            seatInfoBySeatId.setSoldStock(seatPlanStatus.getSoldOutFlag() ? 1 : 0);
                            performService.updateSeatInfo(seatInfoBySeatId);
                        }
                    }
                }
            }
        }
        return Result.success("success");
    }
}
