package org.springblade.app.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.hibernate.validator.constraints.pl.REGON;
import org.springblade.community.entity.Agency;
import org.springblade.community.entity.Residential;
import org.springblade.community.feign.IAgencyClient;
import org.springblade.community.feign.IResidentialClient;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.estate.dto.NoticeDTO;
import org.springblade.estate.feign.INoticeClient;
import org.springblade.estate.feign.INoticePushRecordClient;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.List;

@RestController
@RequestMapping("/parknotice")
@RequiredArgsConstructor
public class NoticeController {

	private final INoticeClient noticeClient;
	private final IAgencyClient agencyClient;
	private final IResidentialClient residentialClient;
	private final INoticePushRecordClient noticePushRecordClient;

	@PostMapping("/save")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "公告新增/编辑")
	public R remoteOpenDoor(@RequestBody NoticeDTO noticeDTO){
		Assert.isTrue(StringUtil.isNoneBlank(noticeDTO.getTitle()), "公告标题不能为空");
		Assert.isTrue(StringUtil.isNotBlank(noticeDTO.getNotice()), "公告内容不能为空");
		Long agencyId = noticeDTO.getAgencyId();
		Agency agency = agencyClient.getById(agencyId);
		Assert.notNull(agency, "发布园区不存在");
		noticeDTO.setOrgPosition(agency.getOrgPosition());
		if(noticeDTO.getResidentialId() != null){
			Residential residential = residentialClient.getById(noticeDTO.getResidentialId());
			if(residential != null){
				noticeDTO.setResidentialId(residential.getId());
				noticeDTO.setResidentialName(residential.getName());
				noticeDTO.setOrgPosition(residential.getOrgPosition());
			}
		}
		noticeDTO.setAgencyName(agency.getName());
//		noticeDTO.setNotice(new String(Base64.getEncoder().encode(noticeDTO.getNotice().getBytes(StandardCharsets.UTF_8))));
		if(noticeDTO.getId() != null){
			boolean update = noticeClient.update(noticeDTO);
			return R.status(update);
		}else{
			boolean submit = noticeClient.publishNotice(noticeDTO);
			return R.status(submit);
		}
	}

	@PostMapping("/remove")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "公告删除")
	public R remove(@RequestParam String ids) {
		return R.status(noticeClient.delete(ids));
	}
}
