package com.ycwl.basic.service.mobile.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ycwl.basic.constant.BaseContextHandler;
import com.ycwl.basic.mapper.*;
import com.ycwl.basic.model.jwt.JwtInfo;
import com.ycwl.basic.model.mobile.scenic.ScenicAppVO;
import com.ycwl.basic.model.mobile.scenic.ScenicDeviceCountVO;
import com.ycwl.basic.model.mobile.scenic.account.ScenicLoginReq;
import com.ycwl.basic.model.mobile.scenic.account.ScenicLoginRespVO;
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.service.mobile.AppScenicService;
import com.ycwl.basic.utils.ApiResponse;
import com.ycwl.basic.utils.JwtTokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author：longbinbin
 * @Date：2024/12/6 10:23
 */
@Slf4j
@Service
public class AppScenicServiceImpl implements AppScenicService {

    @Autowired
    private ScenicMapper scenicMapper;
    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private ScenicAccountMapper scenicAccountMapper;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Override
    public ApiResponse<PageInfo<ScenicAppVO>> pageQuery(ScenicReqQuery scenicReqQuery) {
        PageHelper.startPage(scenicReqQuery.getPageNum(), scenicReqQuery.getPageSize());
        List<ScenicAppVO> list = scenicMapper.appList(scenicReqQuery);
        PageInfo<ScenicAppVO> pageInfo = new PageInfo<>(list);
        return ApiResponse.success(pageInfo);
    }

    @Override
    public ApiResponse<ScenicDeviceCountVO> deviceCountByScenicId(Long scenicId) {
        JwtInfo worker = JwtTokenUtil.getWorker();
        ScenicDeviceCountVO scenicDeviceCountVO = deviceMapper.deviceCountByScenicId(scenicId);
        return ApiResponse.success(scenicDeviceCountVO);
    }

    @Override
    public ApiResponse<ScenicRespVO> getDetails(Long id) {
        ScenicRespVO scenicRespVO = scenicMapper.getAppById(id);
        ScenicDeviceCountVO scenicDeviceCountVO = deviceMapper.deviceCountByScenicId(id);
        scenicRespVO.setLensNum(scenicDeviceCountVO.getTotalDeviceCount());
        return ApiResponse.success(scenicRespVO);
    }

    @Override
    public ApiResponse<ScenicLoginRespVO> login(ScenicLoginReq scenicLoginReq) throws Exception {

        ScenicAccountEntity scenicAccount = scenicAccountMapper.getByAccount(scenicLoginReq.getAccount());
        if (scenicAccount == null) {
            return ApiResponse.fail("账号不存在");
        }
        if (scenicAccount.getStatus() == 0) {
            return ApiResponse.fail("账号已被禁用");
        }
        if (!scenicAccount.getPassword().equals(scenicLoginReq.getPassword())) {
            return ApiResponse.fail("密码错误");
        }

        JwtInfo jwtInfo = new JwtInfo();
        jwtInfo.setName(scenicAccount.getName());
        jwtInfo.setAccount(scenicAccount.getAccount());
        jwtInfo.setUserId(scenicAccount.getId());
        jwtInfo.setScenicId(scenicAccount.getScenicId());
        String token = jwtTokenUtil.generateToken(jwtInfo);

        ScenicLoginRespVO scenicLoginRespVO = new ScenicLoginRespVO();
        BeanUtil.copyProperties(scenicAccount,scenicLoginRespVO);
        scenicLoginRespVO.setToken(token);
        return ApiResponse.success(scenicLoginRespVO);
    }

    @Override
    public ApiResponse<ScenicRespVO> getMyScenic() {
        String userId = BaseContextHandler.getUserId();
        ScenicAccountEntity account = scenicAccountMapper.findAccountById(userId);
        if (account == null) {
            return ApiResponse.fail("用户未绑定景区");
        }
        return getDetails(account.getScenicId());
    }

    @Override
    public ApiResponse<List<DeviceRespVO>> getMyDevices() {
        String userId = BaseContextHandler.getUserId();
        ScenicAccountEntity account = scenicAccountMapper.findAccountById(userId);
        if (account == null) {
            return ApiResponse.fail("用户未绑定景区");
        }
        List<DeviceRespVO> deviceRespVOList = deviceMapper.listByScenicIdWithWVP(account.getScenicId());
        return ApiResponse.success(deviceRespVOList);
    }
}
