package com.ruoyi.bar;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.bar.domain.BarAddressVO;
import com.ruoyi.bar.domain.WyBar;
import com.ruoyi.bar.service.IWyBarService;
import com.ruoyi.bar.service.IWyBarUserService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.WyBarUser;
import com.ruoyi.common.core.domain.entity.WyBreweryUser;
import com.ruoyi.common.core.domain.model.BarLoginUser;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.TencentMapUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 酒吧Controller
 *
 * @author ruoyi
 * @date 2025-01-02
 */
@Api(tags = "酒吧信息管理")
@RestController
@RequestMapping("/bar/barinfo")
public class BarController extends BaseController {
    @Autowired
    private IWyBarService wyBarService;

    /**
     * 获取酒吧信息
     *
     * @return 用户信息
     */
    @ApiOperation("获取酒吧信息")
    @GetMapping("/getBarInfo")
    public AjaxResult getBarInfo() {
        BarLoginUser loginUser = SecurityUtils.getBarLoginUser();
        WyBarUser user = loginUser.getUser();
        WyBar bar = wyBarService.selectWyBarById(user.getBarId());
        return AjaxResult.success(bar);
    }

    /**
     * 修改酒吧
     */
    @ApiOperation("修改酒吧")
    @PutMapping
    public AjaxResult edit(@RequestBody WyBar wyBar) {
        BarLoginUser loginUser = SecurityUtils.getBarLoginUser();
        wyBar.setId(loginUser.getUser().getBarId());
        wyBar.setAuthState(null); //不可编辑
        wyBar.setEditTimes(null); //不可编辑
//        if (bar.getLatitude() == null && wyBar.getLatitude() == null) {
//            throw new IllegalArgumentException("纬度不能为空");
//        }
//        if (bar.getLongitude() == null && wyBar.getLongitude() == null) {
//            throw new IllegalArgumentException("经度不能为空");
//        }
        return toAjax(wyBarService.updateWyBar(wyBar));
    }

    /**
     * 重新认证
     */
    @ApiOperation("重新认证")
    @PostMapping("/reAuth")
    public AjaxResult reAuth(@RequestBody WyBar wyBar) {
        BarLoginUser loginUser = SecurityUtils.getBarLoginUser();
        wyBar.setId(loginUser.getUser().getBarId());
        final WyBar bar = new WyBar();
        bar.setAuthState(0);
        bar.setId(loginUser.getUser().getBarId());
        bar.setBusinessLicense(wyBar.getBusinessLicense());
        bar.setStorefrontPhoto(wyBar.getStorefrontPhoto());
        return toAjax(wyBarService.updateWyBar(wyBar));
    }

    /**
     * 修改酒吧地址
     */
    @ApiOperation("修改酒吧地址")
    @PostMapping("/editAddress")
    public AjaxResult editAddress(@RequestBody BarAddressVO wyBar) throws Exception {
        BarLoginUser loginUser = SecurityUtils.getBarLoginUser();
        final WyBar bar = wyBarService.selectWyBarById(loginUser.getUser().getBarId());
        if (bar.getLatitude() == null && wyBar.getLatitude() == null) {
            throw new IllegalArgumentException("纬度不能为空");
        }
        if (bar.getLongitude() == null && wyBar.getLongitude() == null) {
            throw new IllegalArgumentException("经度不能为空");
        }
        if (bar.getEditTimes() == null || bar.getEditTimes() == 0) {
             throw new IllegalArgumentException("地址修改次数用尽");
        }
        final JSONObject addressComponent = TencentMapUtils.getLatAndLng(bar.getLatitude().toString(), bar.getLongitude().toString());
        if (addressComponent == null) { // 地址解析失败
            throw new IllegalArgumentException("地址解析失败");
        }
//        wyBar.setProvince(addressComponent.getString("province"));
//        wyBar.setCity(addressComponent.getString("city"));
//        wyBar.setDistrict(addressComponent.getString("district"));
        WyBar saveBar = new WyBar();
        saveBar.setId(loginUser.getUser().getBarId());
        saveBar.setAddress(wyBar.getAddress());
        saveBar.setProvince(addressComponent.getString("province"));
        saveBar.setCity(addressComponent.getString("city"));
        saveBar.setDistrict(addressComponent.getString("district"));
        saveBar.setLatitude(wyBar.getLatitude());
        saveBar.setLongitude(wyBar.getLongitude());
        saveBar.setAuthState(null);
        saveBar.setEditTimes(bar.getEditTimes() - 1);
        return toAjax(wyBarService.updateWyBar(saveBar));
    }

    private void validate(Long id) {
        final WyBar wyBar = wyBarService.selectWyBarById(id);
        if (wyBar == null) {
            throw new IllegalArgumentException("酒吧不存在");
        }
        if (!SecurityUtils.getBarLoginUser().getBarId().equals(wyBar.getId())) {
            throw new IllegalArgumentException("只能操作自己的酒吧的信息");
        }
    }

}
