/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ifish.util;

import com.ifish.enums.SubDirectoryEnum;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.Properties;

/**
 *
 * @author Administrator
 */
public class IfishFilePath {

    /**
     * 图片头地址
     */
    public static String link_img_head;
    //图片上传格式
    public static String check_style;

    public static String AppKeyFormal;

    public static String MasterSecretFormal;

    public static String AppKey;

    public static String MasterSecret;
    //开发模式
    public static boolean devModel;

    static {
        Properties prop = new Properties();
        InputStream in = IfishFilePath.class.getResourceAsStream("/property.properties");
        try {
            prop.load(in);
            //利用反射机制给属性赋值
            Field[] fields = IfishFilePath.class.getDeclaredFields();
            for (Field field : fields) {
                String name = field.getName();
                //获取property.properties的值
                String value = prop.getProperty(name).trim();
                //判断这个值希望塞入的变量类型，并进行转换
                Object value1 = getValue(value, field);
                //转换完成后塞入此变量
                field.set(IfishFilePath.class, value1);

            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 验证图片格式
     */
    public static String checkImgName(String fileName) {
        //对格式进行验证
        int index = fileName.lastIndexOf(".");
        String extEndName = fileName.substring(++index, fileName.length());
        if (check_style.indexOf(extEndName.toUpperCase()) == -1) {
            return null;
        }
        //返回格式.png
        return "." + extEndName;
    }

    /**
     * 获取图片链接地址
     *
     * @param type
     * @return
     */
    public static String getPath(SubDirectoryEnum pictureEnum, String fileName) {
        return link_img_head + pictureEnum.getKey() + fileName;
    }

    private static Object getValue(String value, Field field) {
        if (field.getType().equals(int.class)) {
            return Integer.valueOf(value);
        } else if (field.getType().equals(boolean.class)) {
            return Boolean.valueOf(value);
        }
        return value;
    }
}
