package com.org.hktshop.controller.user;

import com.org.hktshop.base.BaseResult;
import com.org.hktshop.controller.BaseController;
import com.org.hktshop.dto.HktUserAddressDto;
import com.org.hktshop.entity.address.Area;
import com.org.hktshop.entity.address.City;
import com.org.hktshop.entity.address.Province;
import com.org.hktshop.entity.address.Street;
import com.org.hktshop.entity.user.HktUser;
import com.org.hktshop.entity.user.HktUserAddress;
import com.org.hktshop.service.address.AreaService;
import com.org.hktshop.service.address.CityService;
import com.org.hktshop.service.address.ProvinceService;
import com.org.hktshop.service.address.StreetService;
import com.org.hktshop.service.user.HktUserAddressService;
import com.org.hktshop.service.user.HktUserService;
import com.org.hktshop.utils.HttpCode;
import com.org.hktshop.utils.IpUtils;
import com.org.hktshop.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/shop/address")
@Api(tags = "用户地址接口")
public class HktUserAddressController extends BaseController {

    @Autowired
    private HktUserAddressService hktUserAddressService;

    @Autowired
    private ProvinceService provinceService;

    @Autowired
    private CityService cityService;

    @Autowired
    private AreaService areaService;

    @Autowired
    private StreetService streetService;

    @Autowired
    private HktUserService userService;

    /**
     * 更新用户地址
     *
     * @param hktUserAddressDto
     * @return
     */
    @PostMapping("/updateUserAddress")
    @ApiOperation(value = "更新用户地址", notes = "更新用户地址")
    public BaseResult updateUserAddress(@RequestBody HktUserAddressDto hktUserAddressDto, HttpServletRequest request) {
        try {
            HktUser user = (HktUser) getSessionUserId(request);
            if(StringUtils.isNull(user.getId())){
                return BaseResult.fail("不是内部账号无法操作");
            }
            HktUserAddress hktUserAddress = new HktUserAddress();
            BeanUtils.copyProperties(hktUserAddressDto, hktUserAddress);
            hktUserAddress.setUserId(user.getId());
            hktUserAddressService.updateHktUserAddress(hktUserAddress);
            return BaseResult.success();
        } catch (Exception ex) {
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(), HttpCode.INTERNAL_SERVER_ERROR.getMsg());
        }
    }
    /**
     * 保存用户地址
     *
     * @param hktUserAddressDto
     * @return
     */
    @PostMapping("/saveUserAddress")
    @ApiOperation(value = "保存用户地址", notes = "保存用户地址")
    public BaseResult saveUserAddress(@RequestBody HktUserAddressDto hktUserAddressDto, HttpServletRequest request) {
        try {
            HktUser user = (HktUser) getSessionUserId(request);
            if(IpUtils.getAddress() == 2){
                HktUser ht = new HktUser();
                ht.setMobile(user.getMobile());
                HktUser hktUser = userService.queryUser(ht);
                if(hktUser == null){
                    return BaseResult.fail("不是内部账号无法添加地址，请联系管理员");
                }
            }
            HktUserAddress hktUserAddress = new HktUserAddress();
            BeanUtils.copyProperties(hktUserAddressDto, hktUserAddress);
            hktUserAddress.setUserId(user.getId());
            hktUserAddressService.updateHktUserAddress(hktUserAddress);
            return BaseResult.success();
        } catch (Exception ex) {
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(), HttpCode.INTERNAL_SERVER_ERROR.getMsg());
        }
    }

    @GetMapping("/setDefaultAddress")
    @ApiOperation(value = "设置用户默认地址", notes = "设置用户默认地址")
    public BaseResult setDefaultAddress(@RequestParam("id") String id, HttpServletRequest request) {
        try {
            HktUser user = (HktUser) getSessionUserId(request);
            hktUserAddressService.setDefaultAddress(user.getId(), id);
            return BaseResult.success();
        } catch (Exception ex) {
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(), HttpCode.INTERNAL_SERVER_ERROR.getMsg());
        }
    }



    @GetMapping("/deleteUserAddress")
    @ApiOperation(value = "通过id删除用户地址信息", notes = "通过id删除用户地址信息")
    @ApiImplicitParam(name = "id", value = "主键id", required = true, paramType = "query")
    public BaseResult deleteUserAddress(@RequestParam("id") String id, HttpServletRequest request) {
        try {
            HktUser user = (HktUser) getSessionUserId(request);
            HktUserAddress hktUserAddress = new HktUserAddress();
            hktUserAddress.setUserId(user.getId());
            hktUserAddress.setId(id);
            hktUserAddressService.deleteHktUserAddress(hktUserAddress);
            return BaseResult.success();
        } catch (Exception ex) {
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(), HttpCode.INTERNAL_SERVER_ERROR.getMsg());
        }
    }

    @GetMapping("/getUserAddress")
    @ApiOperation(value = "获取用户地址信息", notes = "获取用户地址信息")
    public BaseResult<List<HktUserAddress>> getUserAddress(String distribution,HttpServletRequest request) {
        try {
            HktUser user = (HktUser) getSessionUserId(request);
            if(StringUtils.isNull(user.getId())){
                return BaseResult.fail("不是内部账号无法操作");
            }
            List<HktUserAddress> hktUserAddressList = hktUserAddressService.getHktUserAddress(user.getId(),distribution);
            return BaseResult.successWithData(hktUserAddressList);
        } catch (Exception ex) {
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(), HttpCode.INTERNAL_SERVER_ERROR.getMsg());
        }
    }

    @GetMapping("/getAddressInfo")
    @ApiOperation(value = "根据编码获取地址信息", notes = "根据编码获取地址信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "level", value = "层级", required = true, paramType = "query"),
            @ApiImplicitParam(name = "parentCode", value = "上级编码", paramType = "query")
    })
    public BaseResult getAddressInfo(@RequestParam("level") Integer level, @RequestParam("parentCode") String parentCode, HttpServletRequest request) {
        try {
            switch (level) {
                case 0:
                    List<Province> provinceList = provinceService.findAll();
                    return BaseResult.successWithData(provinceList);
                case 1:
                    List<City> cityList = cityService.findCityByProvinceCode(parentCode);
                    return BaseResult.successWithData(cityList);
                case 2:
                    List<Area> areaList = areaService.findAreaByCityCode(parentCode);
                    return BaseResult.successWithData(areaList);
                case 3:
                    List<Street> streetList = streetService.findStreetByAreaCode(parentCode);
                    return BaseResult.successWithData(streetList);
                default:
                    return BaseResult.fail(HttpCode.LEVEL_FAIL.getCode(), HttpCode.LEVEL_FAIL.getMsg());
            }
        } catch (Exception ex) {
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(), HttpCode.INTERNAL_SERVER_ERROR.getMsg());
        }
    }
}
