package com.fs.app.controller;

import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import com.fs.app.annotation.Login;
import com.fs.common.annotation.DataScope;
import com.fs.common.annotation.Log;
import com.fs.common.config.FSConfig;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.R;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.exception.CustomException;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.company.domain.CompanyUser;
import com.fs.company.service.ICompanyUserService;
import com.fs.crm.vo.CrmFullCustomerListQueryVO;
import com.fs.store.domain.FsStorePayment;
import com.fs.store.param.FsStorePaymentParam;
import com.fs.store.service.IFsStorePaymentService;
import com.fs.store.vo.FsStorePaymentVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import static com.fs.common.constant.Constants.PAGE_SIZE;


@RestController
@RequestMapping("/app/storePayment")
public class StorePaymentController extends AppBaseController
{
    @Autowired
    private FSConfig fsConfig;
    @Autowired
    private IFsStorePaymentService fsStorePaymentService;
    @Autowired
    private ICompanyUserService companyUserService;
    @Login
    @GetMapping("/getPaymenyList")
    @DataScope(deptAlias = "p",userAlias = "cu")
    public R list(FsStorePaymentParam param)
    {
        PageHelper.startPage(ServletUtils.getParameterToInt("page"), ServletUtils.getParameterToInt("pageSize"));
        param.setBusinessType(1);
        param.setStatus(1);
        param.setCompanyId(getCompanyId());
        List<FsStorePaymentVO> list = fsStorePaymentService.selectFsStorePaymentListQueryVO(param);
        PageInfo<FsStorePaymentVO> listPageInfo=new PageInfo<>(list);
        return R.ok().put("data",listPageInfo);
    }


    @Login
    @ApiOperation("获取支付宝收款码")
    @GetMapping("/getAlipayQrImg")
    public R getAlipayQrImg(  HttpServletRequest request){
        Long userId=Long.parseLong(getUserId());
        try {
            CompanyUser companyUser=companyUserService.selectCompanyUserById(userId);
            File newFile = new File("qr.jpg");
            File newFileT = new File("simsunb.ttf");
            try {
                InputStream stream =  getClass().getClassLoader().getResourceAsStream("qr.jpg");
                FileUtils.copyInputStreamToFile(stream, newFile);
                // if(!newFile.exists()){
                //     InputStream stream =  getClass().getClassLoader().getResourceAsStream("fx.jpg");
                //     FileUtils.copyInputStreamToFile(stream, newFile);
                // }
                if(!newFileT.exists()){
                    InputStream streamT =  getClass().getClassLoader()
                            .getResourceAsStream("simsunb.ttf");
                    FileUtils.copyInputStreamToFile(streamT, newFileT);
                }
            } catch (IOException e) {

                throw new CustomException(e.getMessage());
            }

            try {
                String url=fsConfig.getQrPath()+"/qr-"+userId+".jpg";
                File outputFile = new File(url);
                if(!outputFile.exists())
                {
                    try {
                        outputFile.createNewFile();

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
//                Font font =  Font.createFont(Font.TRUETYPE_FONT, newFileT);
//                Font f= font.deriveFont(Font.PLAIN,20);
//                ImgUtil.pressText(//
//                        newFile,
//                        outputFile,
//                        companyUser.getNickName()+" 为您服务",
//                        Color.BLACK,
//                        f, //字体
//                        65, //x坐标修正值。 默认在中间，偏移量相对于中间偏移
//                        100, //y坐标修正值。 默认在中间，偏移量相对于中间偏移
//                        0.8f//透明度：alpha 必须是范围 [0.0, 1.0] 之内（包含边界值）的一个浮点数字
//                );
                File qr = new File(fsConfig.getQrPath()+"/qr-"+userId+".png");
                if(!qr.exists())
                {
                    try {
                        qr.createNewFile();

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                QrCodeUtil.generate( "http://alipay.yjf.runtzh.com/#/?companyId="+companyUser.getCompanyId()+"&companyUserId="+companyUser.getUserId(), 200, 200,
                        FileUtil.file(fsConfig.getQrPath()+"/qr-"+userId+".png"));
                ImgUtil.pressImage(
                        newFile,
                        outputFile,
                        ImgUtil.read(qr), //QR图片
                        0, //x坐标修正值。 默认在中间，偏移量相对于中间偏移
                        20, //y坐标修正值。 默认在中间，偏移量相对于中间偏移
                        1f
                );
                return R.ok().put("url","profile/qr/qr-"+userId+".jpg");

            } catch (Exception e) {
                e.printStackTrace();
                return R.error("操作异常");
            }
        } catch (Exception e){

            return R.error("操作异常");
        }
    }

}
