package com.gree.mall.miniapp.logic.user;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper;
import com.gree.mall.miniapp.bean.user.CurrentCompanyWechat;
import com.gree.mall.miniapp.bean.user.UserAddressBean;
import com.gree.mall.miniapp.exception.RemoteServiceException;
import com.gree.mall.miniapp.logic.common.CommonLogic;
import com.gree.mall.miniapp.plus.entity.*;
import com.gree.mall.miniapp.plus.service.FreightTemplateDetailService;
import com.gree.mall.miniapp.plus.service.RegionService;
import com.gree.mall.miniapp.plus.service.UserAddressService;
import com.gree.mall.miniapp.plus.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.function.Function;

@Service
public class AddressLogic {
    @Autowired
    private FreightTemplateDetailService freightTemplateDetailService;
    @Autowired
    private UserAddressService userAddressService;
    @Autowired
    RegionService regionService;
    @Autowired
    private CommonLogic commonLogic;

    @Transactional
    public UserAddress saveAddress(HttpServletRequest request, UserAddressBean userAddressBean) {
        CurrentCompanyWechat currentCompanyWechat = commonLogic.getCurrentCompanyWechat(request);

        UserAddress userAddress = new UserAddress();
        BeanUtils.copyProperties(userAddressBean, userAddress);
        userAddress.setUserAddressId(null);
        Integer count = freightTemplateDetailService.lambdaQuery()
                .eq(StringUtils.isNotBlank(userAddressBean.getProvince()), FreightTemplateDetail::getProvince, userAddressBean.getProvince())
                .eq(StringUtils.isNotBlank(userAddressBean.getCity()), FreightTemplateDetail::getCity, userAddressBean.getCity())
                .eq(FreightTemplateDetail::getCompanyWechatId , currentCompanyWechat.getCurrentCompanyWechatId())
                .count();
        userAddress.setStatus(count > 0);
        userAddress.setCreateTime(new Date());
        if (userAddressBean.getDefaultAddr() != null && userAddressBean.getDefaultAddr()) {
            userAddressService.lambdaUpdate().eq(UserAddress::getUserId, userAddressBean.getUserId())
                    .set(UserAddress::getDefaultAddr, false)
                    .update();
        }
        userAddress.setCompanyWechatId(currentCompanyWechat.getCurrentCompanyWechatId());
        userAddress.setCompanyName(currentCompanyWechat.getCurrentCompanyName());

        Region region = regionService.lambdaQuery()
                .eq(Region::getProvinceName, userAddressBean.getProvince())
                .eq(Region::getCityName, userAddressBean.getCity())
                .eq(Region::getAreaName, userAddressBean.getArea())
                .eq(Region::getName, userAddressBean.getStreet()).one();
        if(region != null){
            userAddress.setProvinceId(region.getProvinceCode());
            userAddress.setCityId(region.getCityCode());
            userAddress.setAreaId(region.getAreaCode());
            userAddress.setStreetId(region.getId());
        }
        userAddressService.save(userAddress);
        return userAddress;
    }

    @Transactional
    public UserAddress updateAddress(HttpServletRequest request , UserAddressBean userAddressBean) {
        CurrentCompanyWechat currentCompanyWechat = commonLogic.getCurrentCompanyWechat(request);

        BeanUtils.copyProperties(userAddressBean, userAddressBean);
        Integer count = freightTemplateDetailService.lambdaQuery()
                .eq(StringUtils.isNotBlank(userAddressBean.getProvince()), FreightTemplateDetail::getProvince, userAddressBean.getProvince())
                .eq(StringUtils.isNotBlank(userAddressBean.getCity()), FreightTemplateDetail::getCity, userAddressBean.getCity())
                .eq(FreightTemplateDetail::getCompanyWechatId , currentCompanyWechat.getCurrentCompanyWechatId())
                .count();
        boolean status = count > 0;
        if (userAddressBean.getDefaultAddr() != null && userAddressBean.getDefaultAddr()) {
            userAddressService.lambdaUpdate().eq(UserAddress::getUserId, userAddressBean.getUserId())
                    .set(UserAddress::getDefaultAddr, false)
                    .update();
        }

        UserAddress userAddress = userAddressService.getById(userAddressBean.getUserAddressId());
        BeanUtil.copyProperties(userAddressBean,userAddress);
        userAddress.setStatus(status);

        Region region = regionService.lambdaQuery()
                .eq(Region::getProvinceName, userAddressBean.getProvince())
                .eq(Region::getCityName, userAddressBean.getCity())
                .eq(Region::getAreaName, userAddressBean.getArea())
                .eq(Region::getName, userAddressBean.getStreet()).one();
        if(region != null){
            userAddress.setProvinceId(region.getProvinceCode());
            userAddress.setCityId(region.getCityCode());
            userAddress.setAreaId(region.getAreaCode());
            userAddress.setStreetId(region.getId());
        }
        userAddress.updateById();

//        userAddressService.lambdaUpdate().eq(UserAddress::getUserAddressId, userAddressBean.getUserAddressId())
//                .set(StringUtils.isNotBlank(userAddressBean.getAddress()), UserAddress::getAddress, userAddressBean.getAddress())
//                .set(StringUtils.isNotBlank(userAddressBean.getProvince()), UserAddress::getProvince, userAddressBean.getProvince())
//                .set(StringUtils.isNotBlank(userAddressBean.getCity()), UserAddress::getCity, userAddressBean.getCity())
//                .set(StringUtils.isNotBlank(userAddressBean.getArea()), UserAddress::getArea, userAddressBean.getArea())
//                .set(StringUtils.isNotBlank(userAddressBean.getHouseNo()), UserAddress::getHouseNo, userAddressBean.getHouseNo())
//                .set(StringUtils.isNotBlank(userAddressBean.getName()), UserAddress::getName, userAddressBean.getName())
//                .set(StringUtils.isNotBlank(userAddressBean.getPhone()), UserAddress::getPhone, userAddressBean.getPhone())
//                .set(UserAddress::getStatus, status)
//                .set(StringUtils.isNotBlank(userAddressBean.getStreet()), UserAddress::getStreet, userAddressBean.getStreet())
//                .set(!Objects.isNull(userAddressBean.getDefaultAddr()), UserAddress::getDefaultAddr, userAddressBean.getDefaultAddr())
//                .update();
        return userAddress;
    }


    public String delAddress(String userAddressId) {

        userAddressService.lambdaUpdate().eq(UserAddress::getUserAddressId, userAddressId).remove();
        return userAddressId;
    }


    public UserAddress addressDetail(String userAddressId) {

        UserAddress userAddress = userAddressService.lambdaQuery().eq(UserAddress::getUserAddressId, userAddressId).one();
        return userAddress;
    }

    public IPage<UserAddress> page(String userId, Integer pageNum, Integer pageSize) {
        return userAddressService.lambdaQuery()
                .eq(StringUtils.isNotBlank(userId), UserAddress::getUserId, userId)
                .orderByDesc(UserAddress::getCreateTime).page(new Page(pageNum, pageSize));

    }


}


