package com.izouma.awesomeadmin.service;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectResult;
import com.izouma.awesomeadmin.util.ImagesUtil;
import com.izouma.awesomeadmin.util.PropertiesFileLoader;

@Service
public class OSSFileService {

    /**
     * 日志对象
     */
    private static Logger logger = Logger.getLogger(OSSFileService.class);


    public  String upload(InputStream fin, String path) {

        logger.info("UpLoadImage :上传照片 开始  ： fin：" + fin + "; phth: " + path);
        // TODO Auto-generated method stub
        String aliid = PropertiesFileLoader.getDefaultProperties("aliossid", "");
        String alikey = PropertiesFileLoader.getDefaultProperties("aliosskey", "");
        String aliossendpoit = PropertiesFileLoader.getDefaultProperties("aliossendpoit", "");
        String bucketname = PropertiesFileLoader.getDefaultProperties("alibucketname", "");
        OSSClient client = new OSSClient(aliossendpoit, aliid, alikey);
        OSSObject object = null;
        logger.info("UpLoadImage :上传照片  ： aliid：" + aliid + "; alikey: " + alikey + "; aliossendpoit : " + aliossendpoit + "; bucketname:"
                + bucketname);
        if (path.startsWith("/")) {
            return "invalid path";
        }
        @SuppressWarnings("unused")
        PutObjectResult result = client.putObject(bucketname, path, fin, new ObjectMetadata());

        try {
            object = client.getObject(bucketname, path);
        } catch (Exception r) {
            logger.error("UpLoadImage: 上传照片异常 ", r);
        }
        // 如果不设置content-length, 默认为chunked编码。
        if (object != null) {
            return GetRealImagePath(path);
        }
        return null;
    }


    public static String GetRealImagePath(String Path) {

        // TODO Auto-generated method stub
        if (Path == null || "".equals(Path) || Path.startsWith("images")) {
            return Path;
        }
        String aliossendpoit = PropertiesFileLoader.getDefaultProperties("aliImageSever", "");
        return aliossendpoit + "/" + Path;
    }


    public String uploadImg(String base64) throws UnsupportedEncodingException {

        String path = "izouma/application/";

        try {
            logger.info("uploadIcon:上传图片");

            //图片原名称
            String imgName = "";
            //图片生成名称
            String fileName = "";
            if (base64 == null) {
                return null;
            }
            String type = ".jpg";
            Date nowDate = new Date();
            //格式化时间对象返回字符串
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
            fileName += sdf.format(nowDate);
            Random random = new Random();
            String randomCode = "";
            for (int i = 0; i < 8; i++) {
                randomCode += Integer.toString(random.nextInt(36), 36);
            }
            fileName += randomCode;
            fileName += type;
            String virtualPath = "";
            try {
                virtualPath = path + "/" + fileName;
                //item.write(new File(uploadPath + fileName));
                String result = upload(ImagesUtil.GenerateImage(base64), virtualPath);
                System.out.println("存入照片：" + result);
                return result;
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("上传图片异常");
        }

        return null;

    }

}
