package cn.teammodel.controller.frontend;

import cn.teammodel.common.R;
import cn.teammodel.model.entity.school.Teacher;
import cn.teammodel.repository.AppraiseRepository;
import cn.teammodel.repository.TeacherRepository;
import cn.teammodel.service.EvaluationService;
import com.itextpdf.text.DocumentException;
import io.swagger.annotations.Api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/")
@Api(tags = "鉴权测试")
public class HelloController {

    @Resource
    private EvaluationService evaluationService;
    @Resource
    private AppraiseRepository appraiseRepository;
    @Resource
    private TeacherRepository teacherRepository;
    @Autowired
    private Environment env;
    
    
    @GetMapping("hello")
    @PreAuthorize("@ss.hasRole('admin')")
    public R<String> hello() {
        System.out.println(SecurityContextHolder.getContext().getAuthentication());
       
        return new R(200, "success","hello world");
    }
    @GetMapping("public/free")
    @PreAuthorize("permitAll()")
    public R<String> free() {
        return new R(200, "success","hello world");
    }
    @GetMapping("public/curr")
    public R<String> curr() {
         
        String curr = env.getProperty("spring.env");
        HashSet<String> set=  new HashSet<String>() ;
        set.add("1595321354");
        List<Teacher> techers=  teacherRepository.findAllByCodeAndIdIn("Base", set);
        return new R(200, techers,curr);
    }
    
    @GetMapping("public/pdf")
    public void freepdf(HttpServletResponse response) throws DocumentException, IOException {        // 设置response参数
//        response.reset();
//        response.setContentType("application/pdf");
//        response.setHeader("Content-disposition",
//                "attachment;filename=report_student_" + System.currentTimeMillis() + ".pdf");
//        ClassPathResource resource = new ClassPathResource("templates/pdf_templates/student_report.pdf");
//        InputStream in = resource.getInputStream();
//        ServletOutputStream os = response.getOutputStream();
//        // 处理 stampter
//        PdfReader pdfReader = new PdfReader(in);
//        PdfStamper stamper = new PdfStamper(pdfReader, os);
//
//        Map<String, String> data = PdfUtil.data();
//        DefaultPieDataset dataset = new DefaultPieDataset( );
//        dataset.setValue( "IPhone 5s" , new Double( 20 ) );
//        dataset.setValue( "SamSung Grand" , new Double( 20 ) );
//        dataset.setValue( "MotoG" , new Double( 40 ) );
//        dataset.setValue( "Nokia Lumia" , new Double( 10 ) );
//
//        JFreeChart pieChart = ChartUtil.pieChart("手机销量统计", dataset);
//        ByteArrayOutputStream bos = new ByteArrayOutputStream();
//        ChartUtils.writeChartAsJPEG(bos, pieChart, 850, 440);
//        // 填充表单
//        PdfUtil.fillPdfForm(stamper, data);
//        PdfUtil.fillImage(stamper, "praiseDistribution", bos.toByteArray());
//        PdfUtil.fillImage(stamper, "criticalDistribution", bos.toByteArray());
//        // 关闭流
//        stamper.setFormFlattening(true);
//        stamper.close();
//        os.close();
//
    }


}