package com.tcl.customerapi;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

import org.apache.commons.codecc.binary.Base64;
import org.json.JSONException;
import org.json.JSONObject;

import com.tcl.factory.thirdparty.ThirdParty;

import android.content.Context;
import android.util.Log;

public class MyJSON {

	public Context ctx;
	public MyJSON(Context value){
		ctx = value;
	}
	 // 创建JSONObject对象  
    public  JSONObject createJSONObject(HotelDataUnit value) {  
    	
    	
        JSONObject result = new JSONObject();  
      
        JSONObject date = new JSONObject(); 
        try {
			date.put(defs.JSON_SOURCE, value.source);
	        date.put(defs.JSON_CHANNEL, value.channel);
	        date.put(defs.JSON_VOLUME, value.volume);
	        date.put(defs.JSON_PICTRUE_MODE, value.pictureMode);
	        date.put(defs.JSON_KEYPAD_LOCK, value.keypadLock);
	        date.put(defs.JSON_MAX_VOLUME, value.maxVolume);
	        date.put(defs.JSON_LOGO, value.advertLogo);
	        date.put(defs.JSON_BOOTANIMATION, value.advertBootanimation);
	        date.put(defs.JSON_AC_MODE, value.acMode);
	        date.put(defs.JSON_REMOTE_DARK, value.remoteDark);
	        date.put(defs.JSON_LED, value.led);
	        date.put(defs.JSON_UART, value.uart);
	        date.put(defs.JSON_CEC, value.cec);
	        date.put(defs.JSON_IP, value.ip);
	        date.put(defs.JSON_LAUNCHER, value.launcher);
	        result.put(value.model, date);
		} catch (JSONException e) {
			e.printStackTrace();
		}
        Log.i("JSON",result+"   to ecode");
        return result;  
    } 
    
	public String EncodeJSON(JSONObject value){
		
		String jsonStr = value.toString();
//		String strBase64 = Base64.encodeToString(jsonStr.getBytes(), Base64.DEFAULT); 
		String strBase64 = enCodeBase(jsonStr);
		return frontBack_Encode(strBase64);
		
	}
	
	public String DecodeJSON(String str){
		String dcodeStr = frontBack_Decode(str);
//		String str2 = new String(Base64.decode(dcodeStr.getBytes(), Base64.DEFAULT)); 
		String str2 = deCodeBase(dcodeStr);
		Log.i("JSON","frontBack_Decode str   "+str2);
		return str2;
	}
	
	private String frontBack_Encode(String input)
	{
		Log.i("JSON", input);
//        String temp = input.substring(input.length() - 3) +  // The last character
//                input.substring(3, input.length() - 3) +  // plus the middle part
//                input.substring(0, 3);
        String temp = input.substring(input.length() - 3) +  // The last character
                input.substring(5, input.length() - 3) +  // plus the middle part
                input.substring(0, 5);
        Log.i("JSON", temp);
        return temp ;
	}
	
	private String frontBack_Decode(String input)
	{
		Log.i("JSON", input);
//        String temp = input.substring(input.length() - 3) +  // The last character
//                input.substring(3, input.length() - 3) +  // plus the middle part
//                input.substring(0, 3);
        String temp = input.substring(input.length() - 5) +  // The last character
                input.substring(3, input.length() - 5) +  // plus the middle part
                input.substring(0, 3);
        Log.i("JSON", temp);
        return temp ;
	}
	
    //base64位的加密状态
    public  String enCodeBase(String str){
        byte[] enCodeBase;
        try {
            enCodeBase = Base64.encodeBase64(str.getBytes("utf-8"));
            String enCode = new String(enCodeBase);
            return enCode;
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "出现异常";
        }
    }
    
    //base64位的解密
    public  String deCodeBase(String str){
        byte[] deCodeBase64;
        try {
            deCodeBase64 = Base64.decodeBase64(str.getBytes("utf-8"));
            String deCode = new String(deCodeBase64);
            return deCode;
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return  "出现异常";
        }
    }
    
    public boolean saveDataToFile(String fileName,String data) {
    	boolean flag = true;
        BufferedWriter writer = null;
        File file = new File("/data/local/"+ fileName + ".json");
        //如果文件不存在，则新建一个
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
                flag = false;
            }
        }
        //写入
        try {
            writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file,false), "UTF-8"));
            writer.write(data);
        } catch (IOException e) {
            e.printStackTrace();
            flag = false;
        }finally {
            try {
                if(writer != null){
                    writer.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
                flag = false;
            }
        }
        return flag;
//       System.out.println("文件写入成功！");
    }
    
    public String getDatafromFile(String fileName) {
        
        String Path="/data/local/" + fileName+ ".json";
           BufferedReader reader = null;
         String laststr = "";
           try {
             FileInputStream fileInputStream = new FileInputStream(Path);
              InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
               reader = new BufferedReader(inputStreamReader);
              String tempString = null;
            while ((tempString = reader.readLine()) != null) {
                 laststr += tempString;
             }
              reader.close();
        } catch (IOException e) {
            e.printStackTrace();
           } finally {
              if (reader != null) {
                  try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                   }
              }
          }
          return laststr;
      }    
}
