package org.springblade.gateway.shop_gateway.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springblade.common.enums.AppConstant;
import org.springblade.common.enums.ResCode;
import org.springblade.common.enums.ShopEnum;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.DigestUtil;
import org.springblade.gateway.shop_gateway.entity.dto.AppAccountDto;
import org.springblade.ldt.account.entity.Account;
import org.springblade.ldt.account.service.IAccountService;
import org.springblade.ldt.shop.entity.Shop;
import org.springblade.ldt.shop.service.IShopService;
import org.springblade.modules.resource.utils.SmsUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author cy-computer
 */
@Api(tags = "商户登录模块")
@RestController
@RequestMapping("/shop/account")
@AllArgsConstructor
public class AppAccountController {

	private IAccountService accountService;
	private IShopService shopService;

	@PostMapping("login")
	@ApiOperation(value = "商户登录")
	@SneakyThrows
	public R login(@RequestBody Account account) throws ServiceException {
		Assert.notNull(account.getPhone(), "手机号不能为空");
		Assert.notNull(account.getSecret(), "密码不能为空");
		Account user = accountService.getOne(new QueryWrapper<Account>().lambda()
			.eq(Account::getPhone, account.getPhone())
			.eq(Account::getType, AppConstant.PLATFORM.SHOP.name()));
		Assert.notNull(user, () -> {
			throw new ServiceException(ResCode.USER_NOT_FOUNT);
		});
		Assert.isTrue(Objects.equals(user.getSecret(), DigestUtil.hex(account.getSecret())), () -> {
			throw new ServiceException(ResCode.USER_PWS_ERROR);
		});
		List<Shop> list = shopService.list(Condition.getQueryWrapper(new Shop()).lambda().eq(Shop::getPersonTel, account.getPhone()));
		return R.data(list);
	}

	@PostMapping("register")
	@ApiOperation(value = "注册账号")
	@SneakyThrows
	public R register(@RequestBody @Valid AppAccountDto appAccountDto) {
		Assert.notNull(appAccountDto.getSecret(), "密码不能为空");
		Account isExit = accountService.getOne(Condition.getQueryWrapper(new Account()).lambda().eq(Account::getPhone, appAccountDto.getPhone()));
		Assert.isNull(isExit, "该手机号已注册");

		Assert.isTrue(SmsUtil.validateMessage(null, appAccountDto.getSmsId(), appAccountDto.getValue(), appAccountDto.getPhone()),
			() -> {
				throw new ServiceException(ResCode.VALIDATE_FAIL);
			});
		Account account = new Account();
		BeanUtil.copyProperties(appAccountDto, account);
		account.setType(AppConstant.PLATFORM.SHOP.name());
		account.setSecret(DigestUtil.hex(account.getSecret()));
		Assert.isTrue(accountService.saveOrUpdate(account), () -> {
			throw new ServiceException(ResCode.SHOP_REGISTER_ERROR);
		});
		return R.data(account);
	}

	@PostMapping("phoneLogin")
	@ApiOperation(value = "手机号登录")
	@SneakyThrows
	public R phoneLogin(@RequestBody @Valid AppAccountDto appAccountDto) {
		this.VerifyCode(appAccountDto);
		List<Shop> list = shopService.list(Condition.getQueryWrapper(new Shop()).lambda().eq(Shop::getPersonTel, appAccountDto.getPhone()));
		return R.data(list);
	}

	@PostMapping("forgetPsw")
	@ApiOperation(value = "忘记密码")
	@SneakyThrows
	public R forgetPsw(@RequestBody @Valid AppAccountDto appAccountDto) {
		Assert.notNull(appAccountDto.getSecret(), "新密码不能为空");
		Account account = this.VerifyCode(appAccountDto);
		account.setSecret(DigestUtil.hex(appAccountDto.getSecret()));
		return R.data(accountService.updateById(account));
	}

	/**
	 * 验证验证码
	 *
	 * @return
	 */
	private Account VerifyCode(AppAccountDto appAccountDto) {
		Account account = accountService.getOne(new QueryWrapper<Account>().lambda()
			.eq(Account::getPhone, appAccountDto.getPhone())
			.eq(Account::getType, AppConstant.PLATFORM.SHOP.name()));
		Assert.notNull(account, () -> {
			throw new ServiceException(ResCode.USER_NOT_FOUNT);
		});
		Assert.isTrue(SmsUtil.validateMessage(null, appAccountDto.getSmsId(), appAccountDto.getValue(), appAccountDto.getPhone()),
			() -> {
				throw new ServiceException(ResCode.VALIDATE_FAIL);
			});
		return account;
	}

}
