package cn.com.capcloud.api.action.myself.system.data;

import cn.com.capcloud.api.action.service.ApiService;
import cn.com.capcloud.api.action.service.MongoService;
import cn.com.capcloud.ccui.core.common.json.JackJSON;
import cn.com.capcloud.ccui.core.common.json.JackJSONArray;
import cn.com.capcloud.ccui.core.common.json.JackJSONObject;
import cn.com.capcloud.core.util.QRCodeUtil;
import cn.com.capcloud.core.util.ReadProperty;
import cn.com.capcloud.core.noentitydb.BaseService;
import cn.com.capcloud.core.noentitydb.DefaultAction;
import cn.com.capcloud.core.system.user.UserService;
import org.joda.time.DateTime;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class DataQRCodeAction extends DefaultAction {
    @Autowired
    private BaseService baseService;
    @Autowired
    private UserService userService;
    @Autowired
    private ApiService apiService;
    @Autowired
    private ReadProperty readProperty;
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private MongoService mongoService;
    private static DataQRCodeAction action;
    @PostConstruct
    public void init(){
        action=this;
        action.baseService=this.baseService;
        action.userService=this.userService;
        action.apiService =this.apiService;
        action.readProperty =this.readProperty;
        action.mongoTemplate=this.mongoTemplate;
        action.mongoService=this.mongoService;
    }

    public Map<String,Object> createQRCode(String jsonStr, JackJSONObject retJson, HttpServletRequest request){
        DateTime d=new DateTime();
        JackJSONObject json= JackJSONObject.fromObject(jsonStr);
        JackJSONObject content=json.getJackJSONObject("content");
        String logoImage = String.valueOf(content.get("logo_image"));//存储位置必须是本地服务器
        String logoSeat = String.valueOf(content.get("logo_seat"));
        String qrCodeContent = String.valueOf(content.get("qr_code_content"));
        String bottomText = String.valueOf(content.get("bottom_text"));
        String bottomTextSize = String.valueOf(content.get("bottom_text_size"));
        String qrCodeSizeStr=String.valueOf(content.get("qr_code_size"));
        String qrCodeColor=String.valueOf(content.get("qr_code_color"));
        if(qrCodeSizeStr.equals("null")||qrCodeSizeStr.length()==0){
            qrCodeSizeStr="300";//默认300x300px
        }
        Integer qrCodeSize = Integer.valueOf(qrCodeSizeStr);
        String uuid = String.valueOf(content.get("uuid"));
        if(bottomTextSize.equals("null")||bottomTextSize.length()==0){
            bottomTextSize="30";//默认30
        }
        if(qrCodeColor.equals("null")||qrCodeColor.length()==0){
            qrCodeColor="0x000000";
        }else{
            qrCodeColor.replace("#","0x");
        }
        String suffix = ".jpg";
        String imageName = uuid+suffix;
        if(bottomText.equals("null")&&bottomText.length()==0){
            bottomText=null;
        }
        String fileDir=action.readProperty.getTempFilePath()+d.toString("yyyyMM");
        File folder = new File(fileDir);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        File f = new File(fileDir +"/"+ imageName);
        File fLogo =null;
        if(!logoImage.equals("[]")&&!logoImage.equals("null")){
            JackJSONArray files=JackJSONArray.fromObject(logoImage);
            JackJSONObject file=files.getJackJSONObject(0);
            String logoFileDir=action.readProperty.getTempFilePath()+file.get("diskFileName");
            fLogo = new File(logoFileDir);
        }
        Integer height=qrCodeSize;
        Integer width=qrCodeSize;
        Map<String,Object> infoMap=new HashMap<>();
        infoMap.put("contents",qrCodeContent);
        infoMap.put("logoSeat",logoSeat);
        infoMap.put("bottomText",bottomText);
        infoMap.put("bottomTextSize",bottomTextSize);
        infoMap.put("height",height);
        infoMap.put("width",width);
        infoMap.put("qrCodeColor",qrCodeColor);
        try {
            QRCodeUtil.drawLogoQRCode(fLogo, f,infoMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<Map<String ,Object>> imageList=new ArrayList<>();
        Map<String,Object> fileMap=new HashMap<>();
        fileMap.put("source", "service");
        fileMap.put("realFileName", imageName);
        fileMap.put("diskFileName", d.toString("yyyyMM")+"/"+imageName);
        fileMap.put("suffix",  suffix);
        fileMap.put("width",  infoMap.get("width"));
        fileMap.put("height",  infoMap.get("height"));//注：此时取的并不是前端传过来的高度，而是drawLogoQRCode()方法里设置的高度（以该方法返会的为准）
        imageList.add(fileMap);
        String imageJsonStr= JackJSON.toJSONString(imageList);
        Map<String,Object> map=new HashMap<>();
        map.put("message","生成成功");
        map.put("data",imageJsonStr);
        return map;
    }

}

