package com.apobates.forum.thrones.admin.controller.form;

import com.apobates.forum.attention.ImageDirectoryEnum;
import com.apobates.forum.attention.core.UploadImagePathConvert;
import com.apobates.forum.core.ImageIOMeta;
import com.apobates.forum.utils.Commons;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Optional;
import java.util.ResourceBundle;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 抽像表单对象[前端]
 * @author xiaofanku
 * @since 20200603
 */
public abstract class ActionForm implements Serializable{
    private String record = "0";
    private String token; // (message="错误代码:1051;抽像的操作标识丢失")
    private String status = "0";
    // 来源地址
    // 只允许是本站地址,例:/product
    private String refer;
    private final static Logger logger = LoggerFactory.getLogger(ActionForm.class);
    
    public String getRecord() {
        return record;
    }
    
    public long getLongRecord() {
        return Long.valueOf(record);
    }
    
    public int getIntegerRecord() {
        return Integer.valueOf(record);
    }
    
    public void setRecord(String record) {
        this.record = record;
    }
    
    public void setRecord(long record) {
        this.record = record + "";
    }
    
    public void setRecord(int record) {
        this.record = record + "";
    }
    
    public String getToken() {
        return token;
    }
    
    public void setToken(String token) {
        this.token = token;
    }
    
    public String getStatus() {
        return status;
    }
    
    public void setStatus(Boolean status) {
        setStatus(covertBooleanToString(status));
    }
    
    public Boolean getBooleanStatus() {
        return covertStringToBoolean(getStatus());
    }
    
    public void setStatus(String status) {
        this.status = status;
    }
    
    public String getRefer() {
        return refer;
    }
    
    public void setRefer(HttpServletRequest request) {
        this.refer = getPrevRequestRefer(request);
    }
    
    public static String getPrevRequestRefer(HttpServletRequest request) {
        String httpReferer = request.getHeader("referer");
        if (null == httpReferer) {
            return "";
        }
        // 包含http://
        // 是否是本站地址
        ResourceBundle gisResource = ResourceBundle.getBundle("global");
        String domain = gisResource.getString("site.domain");
        if (null == domain) {
            return "";
        }
        if (!httpReferer.contains(domain)) {
            return "";
        }
        return httpReferer.substring(domain.length() - 1);
    }
    
    /**
     * 是添加还是编辑
     *
     * @return
     */
    public boolean isUpdate() {
        return Long.valueOf(record) > 0;
    }
    
    protected String covertBooleanToString(boolean booleanValue) {
        return booleanValue ? "1" : "0";
    }
    
    protected boolean covertStringToBoolean(String value) {
        return Commons.isNotBlank(value) && "1".equals(value);
    }
    
    protected int covertStringToInteger(String value, int defaultValue) {
        return Commons.stringToInteger(value, defaultValue);
    }
    
    protected long covertStringToLong(String value, long defaultValue) {
        return Commons.stringToLong(value, defaultValue);
    }
    
    public String getActionTitle() {
        return isUpdate() ? "编辑" : "新增";
    }
    
    public String getRequestIpAddr(HttpServletRequest request) {
        return Commons.getRequestIp(request);
    }
    /**
     * 
     * @param imageIOMeta 图片存储信息
     * @param imgBucketURL 上传成功的访问地址
     * @return 
     */
    public Optional<String> encodeUploadImageURL(ImageIOMeta imageIOMeta, String imgBucketURL){
        try{
            return new UploadImagePathConvert(imageIOMeta.getImageBucketDomain(), imageIOMeta.getUploadImageDirectName()).encode(new URL(imgBucketURL), ImageDirectoryEnum.DEFAT);
        }catch(MalformedURLException e){
            if(logger.isDebugEnabled()){
                logger.debug("编码上传图片地址失败", e);
            }
        }
        return Optional.empty();
    }
}