package com.pack;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import org.jdom.Element;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.CannedAccessControlList;
import com.taurus.core.entity.ITObject;
import com.taurus.core.entity.TObject;

import redis.clients.jedis.Jedis;

public class Utils {
	
	public static final String[] svr_list= {"local","test","pro"};
	
	public static ITObject getGameData(Jedis jedis1,int game_id,boolean version_add) {
		List<String> list =  jedis1.hmget("game:"+game_id,"id","name","bundle","version");
		ITObject obj = TObject.newInstance();
		obj.putInt("game_id", Integer.parseInt(list.get(0)));
		obj.putUtfString("name", list.get(1));
		obj.putUtfString("bundle", list.get(2));
		String version = list.get(3);
		if(version_add) {
			String tem[] = version.split("\\.");
			version = tem[0]+"."+tem[1]+"."+(Integer.parseInt(tem[2])+1);
		}
		obj.putUtfString("version", version);
		return obj;
	}
	
	public static byte[] httpGet(String url) {
		try {
			URL realUrl = new URL(url);
			// 打开和URL之间的连接
			HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
			if(conn.getResponseCode() == 200) {
				InputStream is = conn.getInputStream();
				ByteArrayOutputStream o = new ByteArrayOutputStream(1024);  
		        int i = 1024;  
		        byte[] buf = new byte[i];  

		        while ((i = is.read(buf, 0, i)) > 0) {  
		            o.write(buf, 0, i);  
		        }
		        return o.toByteArray();  
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	
	public static void uploadOssPack(String objectName,String path) throws Exception{
		List<?> list = MainServer.ossListConfig.getChildren();
		for (Object o : list) {
			Element ossConfig = (Element)o;
			String endpoint = ossConfig.getChildTextTrim("endpoint");
			String accessKeyId = ossConfig.getChildTextTrim("accessKeyId");
			String accessKeySecret = ossConfig.getChildTextTrim("accessKeySecret");
			String bucketName = ossConfig.getChildTextTrim("bucketName");
			// 创建OSSClient实例。
			OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
			FileInputStream is = new FileInputStream(path+"/"+objectName);
			String temName = MainServer.probjectName+"/" + objectName;
			ossClient.putObject(bucketName, temName, is);
			is.close();
			ossClient.setObjectAcl(bucketName, temName, CannedAccessControlList.PublicRead);
			// 关闭OSSClient。
			ossClient.shutdown();
		}
		
	}
	
	
	public static void uploadOss(Element ossConfig,String objectName,byte[] bytes) throws Exception{
		String endpoint = ossConfig.getChildTextTrim("endpoint");
		String accessKeyId = ossConfig.getChildTextTrim("accessKeyId");
		String accessKeySecret = ossConfig.getChildTextTrim("accessKeySecret");
		String bucketName = ossConfig.getChildTextTrim("bucketName");
		// 创建OSSClient实例。
		OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
		ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(bytes));
		ossClient.setObjectAcl(bucketName, objectName, CannedAccessControlList.PublicRead);
		// 关闭OSSClient。
		ossClient.shutdown();
	}
}
