package com.aircraft.modules.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.aircraft.exception.BadRequestException;
import com.aircraft.modules.aircraft.domain.AircraftDevice;
import com.aircraft.modules.aircraft.service.AircraftDeviceService;
import com.aircraft.modules.route.domain.CpRoute;
import com.aircraft.modules.route.service.CpRouteService;
import com.aircraft.modules.system.domain.CnCustomer;
import com.aircraft.modules.system.domain.EmEmployees;
import com.aircraft.modules.system.domain.EmScenic;
import com.aircraft.modules.system.domain.User;
import com.aircraft.modules.system.domain.vo.KeyValueEntity;
import com.aircraft.modules.system.service.*;
import com.aircraft.utils.enums.UserTypeEnum;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author chenxiky
 * @version 1.0.0
 * @since 2025/7/27
 */
@Service
public class KeyValueEntityServiceImpl implements KeyValueEntityService {

    @Resource
    private UserService userService;
    @Resource
    private CnCustomerService customerService;
    @Resource
    private EmEmployeesService employeesService;
    @Resource
    private EmScenicService scenicService;
    @Resource
    private AircraftDeviceService deviceService;
    @Resource
    private CpRouteService routeService;

    @Override
    public List<KeyValueEntity> obtainUserListByType(Integer userType) {
        // 获取用户列表校验
        UserTypeEnum userTypeEnum = UserTypeEnum.getByValue(userType);
        if (userTypeEnum == null) {
            throw new BadRequestException("用户类型错误");
        }
        List<KeyValueEntity> keyValueEntityList = new ArrayList<>();

        switch (userTypeEnum) {
            case ADMIN:
                keyValueEntityList = obtainAdminUserList();
                break;
            case EMPLOYEES:
                keyValueEntityList = obtainEmployeesUserList();
                break;
            case CUSTOMER:
                keyValueEntityList = obtainCustomerUserList();
                break;
            default:
                break;
        }
        return keyValueEntityList;
    }

    @Override
    public List<KeyValueEntity> obtainScenicList() {
        List<EmScenic> list = scenicService.list(Wrappers.lambdaQuery(EmScenic.class).select(EmScenic::getId, EmScenic::getName).orderByAsc(EmScenic::getId));
        if (CollectionUtil.isEmpty( list)) {
            return new ArrayList<>();
        }
        return list.stream().map(employees -> KeyValueEntity.builder().key(employees.getId()).value(employees.getName())
                .build()).collect(Collectors.toList());
    }

    @Override
    public List<KeyValueEntity> obtainDeviceList(Long attractionId) {
        List<AircraftDevice> list = deviceService.list(Wrappers.lambdaQuery(AircraftDevice.class)
                .select(AircraftDevice::getId, AircraftDevice::getName)
                        .eq(null != attractionId,AircraftDevice::getScenicId,attractionId)
                .orderByAsc(AircraftDevice::getId));
        if (CollectionUtil.isEmpty( list)) {
            return new ArrayList<>();
        }
        return list.stream().map(device -> KeyValueEntity.builder().key(device.getId()).value(device.getName()).build()).collect(Collectors.toList());
    }

    @Override
    public List<KeyValueEntity> obtainAllRoutes() {
        List<CpRoute> list = routeService.obtainAllRoutes();
        if (CollectionUtil.isEmpty( list)) {
            return new ArrayList<>();
        }
        return list.stream().map(route -> KeyValueEntity.builder().key(route.getId()).value(route.getName()).build())
                .collect(Collectors.toList());
    }

    @Override
    public List<KeyValueEntity> obtainRouteListByScenicId(Long scenicId) {
        List<CpRoute> list = routeService.obtainAllRoutes();
        if (CollectionUtil.isEmpty( list)) {
            return new ArrayList<>();
        }
        return list.stream().filter(route -> route.getScenicId().equals(scenicId))
                .map(route -> KeyValueEntity.builder().key(route.getId()).value(route.getName()).build())
                .collect(Collectors.toList());
    }

    /**
     * 获取用户列表
     *
     * @return  List<KeyValueEntity>
     */
    private List<KeyValueEntity> obtainCustomerUserList() {
        List<CnCustomer> list = customerService.list(Wrappers.lambdaQuery(CnCustomer.class)
                .select(CnCustomer::getId, CnCustomer::getName).orderByAsc(CnCustomer::getId));
        if (CollectionUtil.isEmpty( list)){
            return new ArrayList<>();
        }
        return list.stream().map(customer -> KeyValueEntity.builder().key(customer.getId()).value(customer.getName()).build())
                .collect(Collectors.toList());
    }

    /**
     * 获取飞行员端用户列表
     *
     * @return  List<KeyValueEntity>
     */
    private List<KeyValueEntity> obtainEmployeesUserList() {
        List<EmEmployees> list = employeesService.list(Wrappers.lambdaQuery(EmEmployees.class)
                .select(EmEmployees::getId, EmEmployees::getName).orderByAsc(EmEmployees::getId));
        if (CollectionUtil.isEmpty( list)){
            return new ArrayList<>();
        }
        return list.stream().map(employees -> KeyValueEntity.builder().key(employees.getId()).value(employees.getName()).build())
                .collect(Collectors.toList());
    }

    /**
     * 后台端用户下拉值
     *
     * @return  List<KeyValueEntity>
     */
    private List<KeyValueEntity> obtainAdminUserList() {
        List<User> list = userService.list(Wrappers.lambdaQuery(User.class).select(User::getId, User::getNickName).orderByAsc(User::getId));
        if (CollectionUtil.isEmpty( list)) {
            return new ArrayList<>();
        }
        return list.stream().map(user -> KeyValueEntity.builder().key(user.getId()).value(user.getNickName()).build())
                .collect(Collectors.toList());
    }

}
