package com.crm.manager.controller;

import com.crm.manager.dto.export.ActivityDubaiExhibitionExportDto;
import com.crm.manager.service.activity.ActivityDubaiExhibitionFeignService;
import com.crm.rely.backend.core.dto.activity.ActivityDubaiExhibitionCountryConfigDto;
import com.crm.rely.backend.core.dto.activity.ActivityDubaiExhibitionDto;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.base.BaseResultWithPagerDto;
import com.crm.rely.backend.core.entity.activity.ActivityDubaiExhibitionListSearchEntity;
import com.crm.rely.backend.core.entity.custom.info.InfoEntity;
import com.crm.rely.backend.exception.ServiceException;
import com.crm.rely.backend.util.ExportUtil;
import com.crm.rely.backend.util.QRCodeImgeUtil;
import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/activity/dubai/exhibition")
public class ActivityDubaiExhibitionController {

    @Autowired
    private ActivityDubaiExhibitionFeignService activityDubaiExhibitionFeignService;

    @PostMapping("/search/list")
    public BaseResultWithPagerDto<List<ActivityDubaiExhibitionDto>> searchList(@RequestBody ActivityDubaiExhibitionListSearchEntity entity,InfoEntity infoEntity) throws ServiceException {
        entity.setUserEmail(infoEntity.getIbInfo().getEmail());
        return activityDubaiExhibitionFeignService.searchList(entity);
    }

    @GetMapping("/export")
    public void export(@RequestParam(name = "name") String name,
                       @RequestParam(name = "email") String email,
                       @RequestParam(name = "number") String number,
                       @RequestParam(name = "referrer") String referrer,
                       @RequestParam(name = "countryCode") String countryCode,
                       @RequestParam(name = "startDate", required = false) String startDate,
                       @RequestParam(name = "endDate", required = false) String endDate,
                       InfoEntity infoEntity, HttpServletResponse response) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        ActivityDubaiExhibitionListSearchEntity entity = new ActivityDubaiExhibitionListSearchEntity();
        entity.setName(name);
        entity.setEmail(email);
        entity.setNumber(number);
        entity.setReferrer(referrer);
        entity.setCountryCode(countryCode);
        entity.setStartDate(startDate == null ? null : simpleDateFormat.parse(startDate));
        entity.setEndDate(endDate == null ? null : simpleDateFormat.parse(endDate));
        entity.setPage(null);
        BaseResultWithPagerDto<List<ActivityDubaiExhibitionDto>> resultDto = activityDubaiExhibitionFeignService.searchList(entity);
        if (resultDto.getCode() == 200) {
            List<ActivityDubaiExhibitionExportDto> dtos = Lists.newArrayList();
            resultDto.getData().forEach((item) -> {
                ActivityDubaiExhibitionExportDto dto = new ActivityDubaiExhibitionExportDto();
                BeanUtils.copyProperties(item, dto);
                dtos.add(dto);
            });
            ExportUtil.transferToResponse(genFileName("DUBAI_EXHIBITION"), dtos, ActivityDubaiExhibitionExportDto.class, response);

        }
    }

    @PostMapping("/countries")
    BaseResultDto<List<ActivityDubaiExhibitionCountryConfigDto>> countryConfig() throws ServiceException {
        return activityDubaiExhibitionFeignService.countryConfig();
    }

    @PostMapping("/qrCode")
    public BaseResultDto<Object> qrCode(@RequestBody ActivityDubaiExhibitionListSearchEntity entity,
                       InfoEntity infoEntity, HttpServletResponse response) throws Exception {

        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("?referrer=").append(infoEntity.getIbInfo().getIbNo()).append("&countryCode=").append(entity.getCountryCode());

        return BaseResultDto.success((Object) stringBuffer.toString());
    }


    private String genFileName(String fileNamePrefix) {
        StringBuffer stringBuffer = new StringBuffer();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        stringBuffer.append(fileNamePrefix);
        stringBuffer.append("_");
        stringBuffer.append(sdf.format(new Date()));
        stringBuffer.append(".xlsx");
        String fileName = stringBuffer.toString();
        return fileName;
    }

}
