package com.ycwl.basic.controller.mobile.manage;

import com.ycwl.basic.annotation.IgnoreToken;
import com.ycwl.basic.constant.BaseContextHandler;
import com.ycwl.basic.integration.scenic.dto.scenic.ScenicV2DTO;
import com.ycwl.basic.model.mobile.scenic.account.ScenicLoginOldRespVO;
import com.ycwl.basic.model.mobile.scenic.account.ScenicLoginReq;
import com.ycwl.basic.model.mobile.scenic.account.ScenicLoginRespVO;
import com.ycwl.basic.model.mobile.scenic.account.ScenicRegisterReq;
import com.ycwl.basic.model.mobile.scenic.account.ScenicRegisterRespVO;
import com.ycwl.basic.model.mobile.weChat.DTO.WeChatUserInfoDTO;
import com.ycwl.basic.model.pc.device.resp.DeviceRespVO;
import com.ycwl.basic.model.pc.scenic.entity.ScenicAccountEntity;
import com.ycwl.basic.model.pc.scenic.req.ScenicReqQuery;
import com.ycwl.basic.model.pc.scenic.resp.ScenicRespVO;
import com.ycwl.basic.repository.ScenicRepository;
import com.ycwl.basic.service.mobile.AppScenicService;
import com.ycwl.basic.service.pc.ScenicAccountService;
import com.ycwl.basic.service.pc.ScenicService;
import com.ycwl.basic.utils.ApiResponse;
import org.apache.commons.lang3.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;

import static com.ycwl.basic.constant.JwtRoleConstant.ADMIN;
import static com.ycwl.basic.constant.JwtRoleConstant.MERCHANT;

/**
 * @Author：longbinbin
 * @Date：2024/12/12 18:28
 */
@RestController
@RequestMapping("/api/mobile/scenicAccount/v1")
// 景区账号相关接口
public class AppScenicAccountController {
    @Autowired
    private ScenicAccountService accountService;
    @Autowired
    private AppScenicService scenicService;
    @Autowired
    private ScenicService adminScenicService;
    @Autowired
    private ScenicRepository scenicRepository;

    // 登录
    @PostMapping("/login")
    @IgnoreToken
    public ApiResponse<ScenicLoginOldRespVO> login(@RequestBody ScenicLoginReq scenicLoginReq) throws Exception {
        ApiResponse<ScenicLoginRespVO> logined = scenicService.login(scenicLoginReq);
        ScenicLoginOldRespVO vo = new ScenicLoginOldRespVO();
        if (!logined.isSuccess()) {
            return ApiResponse.fail(logined.getMsg());
        }
        vo.setId(logined.getData().getId());
        vo.setScenicId(logined.getData().getScenicId().getFirst());
        vo.setIsSuper(logined.getData().getIsSuper());
        vo.setName(logined.getData().getName());
        vo.setAccount(logined.getData().getAccount());
        vo.setStatus(logined.getData().getStatus());
        vo.setToken(logined.getData().getToken());
        return ApiResponse.success(vo);
    }

    // 注册
    @PostMapping("/register")
    @IgnoreToken
    public ApiResponse<ScenicRegisterRespVO> register(@RequestBody ScenicRegisterReq scenicRegisterReq) {
        return scenicService.register(scenicRegisterReq);
    }

    @GetMapping("/myScenicList")
    public ApiResponse<List<ScenicV2DTO>> myScenicList() {
        List<ScenicV2DTO> list = Collections.emptyList();
        if (Strings.CS.equals(BaseContextHandler.getRoleId(), MERCHANT.type)) {
            String userId = BaseContextHandler.getUserId();
            ScenicAccountEntity account = accountService.getScenicAccountById(Long.valueOf(userId));
            if (account == null || account.getScenicId().isEmpty()) {
                return ApiResponse.fail("景区账号未绑定景区");
            }
            list = account.getScenicId().stream()
                    .map(id -> scenicRepository.getScenicBasic(id))
                    .toList();
        } else if (Strings.CS.equals(BaseContextHandler.getRoleId(), ADMIN.type)) {
            ScenicReqQuery query = new ScenicReqQuery();
            query.setPageSize(1000);
            list = scenicRepository.list(query);
        }
        return ApiResponse.success(list);
    }

    @GetMapping("/getScenic")
    public ApiResponse<ScenicRespVO> getMyScenic() {
        String userId = BaseContextHandler.getUserId();
        ScenicAccountEntity account = accountService.getScenicAccountById(Long.valueOf(userId));
        if (account == null || account.getScenicId().isEmpty()) {
            return ApiResponse.fail("景区账号未绑定景区");
        }
        return scenicService.getDetails(account.getScenicId().getFirst());
    }

    @GetMapping("/{scenicId}")
    public ApiResponse<ScenicRespVO> getScenic(@PathVariable Long scenicId) {
        String userId = BaseContextHandler.getUserId();
        ScenicAccountEntity account = accountService.getScenicAccountById(Long.valueOf(userId));
        if (account == null || account.getScenicId().isEmpty()) {
            return ApiResponse.fail("景区账号未绑定景区");
        }
        if (!account.getScenicId().contains(scenicId)) {
            return ApiResponse.fail("无权限");
        }
        return scenicService.getDetails(scenicId);
    }

    @GetMapping("/devices")
    @Deprecated
    public ApiResponse<List<DeviceRespVO>> getDeviceList() {
        String userId = BaseContextHandler.getUserId();
        ScenicAccountEntity account = accountService.getScenicAccountById(Long.valueOf(userId));
        if (account == null || account.getScenicId().isEmpty()) {
            return ApiResponse.fail("景区账号未绑定景区");
        }
        return scenicService.getDevices(account.getScenicId().getFirst());
    }

    @GetMapping("/{scenicId}/devices")
    public ApiResponse<List<DeviceRespVO>> getDeviceList(@PathVariable Long scenicId) {
        String userId = BaseContextHandler.getUserId();
        ScenicAccountEntity account = accountService.getScenicAccountById(Long.valueOf(userId));
        if (account == null || account.getScenicId().isEmpty()) {
            return ApiResponse.fail("景区账号未绑定景区");
        }
        if (!account.getScenicId().contains(scenicId)) {
            return ApiResponse.fail("无权限");
        }
        return scenicService.getDevices(scenicId);
    }

}
