package com.fzm.mall.server.front.user.controller;

import com.alibaba.fastjson.JSONObject;
import com.fzm.mall.server.front.base.ResponseVO;
import com.fzm.mall.server.front.constant.MallResponseEnum;
import com.fzm.mall.server.front.goods.mapper.CountMapper;
import com.fzm.mall.server.front.user.model.*;
import com.fzm.mall.server.front.user.model.vo.AddrVo;
import com.fzm.mall.server.front.user.model.vo.DrawCoinAddrVo;
import com.fzm.mall.server.front.user.model.vo.LoginVo;
import com.fzm.mall.server.front.user.model.vo.MailAddressVO;
import com.fzm.mall.server.front.user.service.IDrawCoinAddrService;
import com.fzm.mall.server.front.user.service.IMailAddressService;
import com.fzm.mall.server.front.user.service.IUserService;
import com.fzm.mall.server.front.util.CheckUtil;
import com.fzm.mall.server.front.util.PhoneFormatCheckUtils;
import io.netty.util.internal.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.lang.reflect.Field;
import java.util.List;

@Api("地址模块")
@RestController
@RequestMapping(value ="mall/addr",produces = "application/json;charset=UTF-8")
public class AddrController {

    @Autowired
    private IUserService userService;

    @Autowired
    private IMailAddressService mailAddressService;

    @Autowired
    private IDrawCoinAddrService drawCoinAddrService;

    @ApiOperation(value = "获取全部省份")
    @PostMapping("/without/getProvince")
    public ResponseVO<List<Province>> getProvince(){
        List<Province> provinces = userService.getProvinceList();
        return  new ResponseVO<>(MallResponseEnum.SUCCESS,provinces);
    }

    @ApiOperation(value = "获取城市 参数  code")
    @PostMapping("/without/getCity")
    public ResponseVO<List<City>> getCity(@RequestBody JSONObject data){
        Long code = data.getLong("code");
        List<City> citys = userService.getCityList(code);
        return  new ResponseVO<>(MallResponseEnum.SUCCESS,citys);
    }

    @ApiOperation(value = "获取区 参数  code")
    @PostMapping("/without/getCounty")
    public ResponseVO<List<County>> getCounty(@RequestBody JSONObject data){
        Long code = data.getLong("code");
        List<County> countys = userService.getCounty(code);
        return  new ResponseVO<>(MallResponseEnum.SUCCESS,countys);
    }

    @ApiOperation(value = "获取街道 参数  code")
    @PostMapping("/without/getStreet")
    public ResponseVO<List<Street>> getStreet(@RequestBody JSONObject data){
        Long code = data.getLong("code");
        List<Street> streets = userService.getStreet(code);
        return  new ResponseVO<>(MallResponseEnum.SUCCESS,streets);
    }

    @ApiOperation(value = "获取地址")
    @PostMapping("/getAddr")
    public ResponseVO<List<AddrVo>> getAddr(@ApiIgnore @RequestAttribute Header header){
        String uid = header.getUid();
        List<AddrVo> mailAddresses = mailAddressService.getListByUid(uid);
        return new ResponseVO<>(MallResponseEnum.SUCCESS,mailAddresses);
    }

    @ApiOperation(value = "添加地址")
    @PostMapping("/addAddr")
    public ResponseVO<List<MailAddress>> addAddr(@ApiIgnore @RequestAttribute Header header,@RequestBody MailAddressVO mailAddressVo){
        String uid = header.getUid();
        if(mailAddressVo.getProvinceCode() == null){
            return  new ResponseVO<>(MallResponseEnum.PARAMETER_ERR);
        }
        List<City> citys = userService.getCityList(mailAddressVo.getProvinceCode());
        if(citys.size()!=0){
            if(mailAddressVo.getCityCode()==null){
                return  new ResponseVO<>(MallResponseEnum.PARAMETER_ERR);
            }else{
                List<County> countys = userService.getCounty(mailAddressVo.getCityCode());
                if(countys.size()!=0){
                    if(mailAddressVo.getCountyCode() == null){
                        return  new ResponseVO<>(MallResponseEnum.PARAMETER_ERR);
                    }else{
                        List<Street> streets = userService.getStreet(mailAddressVo.getCountyCode());
                        if(streets.size()!=0){
                            if(mailAddressVo.getStreetCode()==null){
                                return  new ResponseVO<>(MallResponseEnum.PARAMETER_ERR);
                            }
                        }else{
                            mailAddressVo.setStreetCode(0l);
                        }
                    }
                }else{
                    mailAddressVo.setCountyCode(0l);
                    mailAddressVo.setStreetCode(0l);
                }
            }
        }else{
            mailAddressVo.setStreetCode(0l);
            mailAddressVo.setCountyCode(0l);
            mailAddressVo.setStreetCode(0l);
        }
        if(!PhoneFormatCheckUtils.isChinaPhoneLegal(mailAddressVo.getPhone())) {
            return new ResponseVO<>(MallResponseEnum.ACCOUNT_ERR);
        }
        MailAddress mailAddress = new MailAddress();
        BeanUtils.copyProperties(mailAddressVo,mailAddress);
        mailAddress.setUid(uid);
        mailAddressService.saveAndUpdate(mailAddress);
        return  new ResponseVO<>(MallResponseEnum.SUCCESS);
    }
    @ApiOperation(value = "删除邮寄地址 参数类型integer   参数名称id")
    @PostMapping("/delAddr")
    public ResponseVO<String> delAddr(@ApiIgnore @RequestAttribute Header header,@RequestBody JSONObject data){
        Integer id = data.getInteger("id");
        if(id == null){
            return  new ResponseVO<>(MallResponseEnum.PARAMETER_ERR);
        }
        mailAddressService.deleteByIdAndUid(id,header.getUid());
        return new ResponseVO<>(MallResponseEnum.SUCCESS);
    }

    @ApiOperation(value = "获取转币地址")
    @PostMapping("/getTranAddr")
    public ResponseVO<List<DrawCoinAddr>> getTranAddr(@ApiIgnore @RequestAttribute Header header){
        String uid = header.getUid();
        List<DrawCoinAddr> list = drawCoinAddrService.getList(uid);
        return  new ResponseVO<>(MallResponseEnum.SUCCESS,list);
    }

    @ApiOperation(value = "添加转币地址")
    @PostMapping("/addTranAddr")
    public ResponseVO<String> addTranAddr(@ApiIgnore @RequestAttribute Header header,@RequestBody DrawCoinAddrVo drawCoinAddrvo){
        if(!CheckUtil.checkObjAllFieldsIsNull(drawCoinAddrvo)){
            return new ResponseVO<>(MallResponseEnum.PARAMETER_ERR);
        }
        DrawCoinAddr drawCoinAddr = new DrawCoinAddr();
        BeanUtils.copyProperties(drawCoinAddrvo,drawCoinAddr);
        String uid = header.getUid();
        drawCoinAddr.setUid(header.getUid());
        drawCoinAddrService.save(drawCoinAddr);
        return  new ResponseVO<>(MallResponseEnum.SUCCESS);
    }

    @ApiOperation(value = "删除转币地址 参数类型integer   参数名称id")
    @PostMapping("/delTranAddr")
    public ResponseVO<String> delTranAddr(@ApiIgnore @RequestAttribute Header header,@RequestBody JSONObject data){
        Integer id =  data.getInteger("id");
        if(id == null){
            return  new ResponseVO<>(MallResponseEnum.PARAMETER_ERR);
        }
        DrawCoinAddr coinAddr = drawCoinAddrService.getByIdAndUid(id,header.getUid());
        if(coinAddr == null){
            return  new ResponseVO<>(MallResponseEnum.SUCCESS);
        }
        drawCoinAddrService.removeById(id);
        return new ResponseVO<>(MallResponseEnum.SUCCESS);
    }
}
