package com.dbs.consentServer.util;

import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.zip.GZIPInputStream;

public class CommonUtils {

  public static String logTime() {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = new Date();
    String strDate = format.format(date);
    return strDate;
  }

  public static boolean isNumeric(String s) {
    try {
      // Double.parseDouble(s);
      Integer.parseInt(s);
      return true;
    } catch(NumberFormatException e) {
      e.printStackTrace();
      return false;
    }
  }

  //   /**
  // * base64Encode
  // * @author jksong
  // * @Param
  // * @since 2019-09-09 오전 8:29
  // **/
  // public static String base64Encode(String text) throws UnsupportedEncodingException {
  //   byte[] data = text.getBytes("UTF-8");)
  //   return Base64.encode(data);
  //   //return Base64.encodeToString(data, Base64.NO_WRAP);     //Android에서 Base64 인코딩 시에 개행문자를 삽입하지 않는 옵션은 NO_WRAP입니다.
  // }
  
  // /**
  //  * base64Encode
  //  * @author jksong
  //  * @Param
  //  * @since 2019-09-09 오전 8:29
  //  **/
  // public static String base64Encode(byte[] digest) throws UnsupportedEncodingException {
  //   return Base64.encode(digest);
  // }

  public static byte[] base64Decode(String encoded) {
    return Base64.getDecoder().decode(encoded);
  }

      /**
  * GZIPInputStream을 이용하여 byte배열 압축해제하기
  * @author jksong
  * @Param
  * @since 2019-09-09 오전 8:30
  **/
  public static String unzipStringFromBytes(byte[] bytes) throws IOException {
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
    GZIPInputStream gzipInputStream = new GZIPInputStream(byteArrayInputStream);
    BufferedInputStream bufferedInputStream = new BufferedInputStream(gzipInputStream);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    
    byte[] buffer = new byte[100];
    int length;
    while((length = bufferedInputStream.read(buffer)) > 0) {
      byteArrayOutputStream.write(buffer, 0, length);
    }
    
    bufferedInputStream.close();
    gzipInputStream.close();
    byteArrayInputStream.close();
    byteArrayOutputStream.close();
    
    return byteArrayOutputStream.toString();
  }
}