package com.ericsson.uecontrol.gui.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.preference.PreferenceManager;
import android.telephony.TelephonyManager;

import org.apache.log4j.Logger;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;

/**
 * Created by ezivkoc on 2014-07-30.
 */
public class CSVWriter {
    private static final Logger log = Logger.getLogger(CSVWriter.class);
    public static final String[] HEADINGS = new String[]{
            "Timestamp", "Behaviour", "RX Throughput(b/s)", "TX Throughput(b/s)", "RAT"
    };
    public static final String DELIMITER = ";";
    protected static final SimpleDateFormat fileDateFormater = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
    protected static final SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");


    private File file;
    private Context context;

    public CSVWriter(Context context){
        this.context = context;
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        String path = prefs.getString("logging_path", "/sdcard/uecontrol/");
        if(!path.endsWith(File.separator)) path += File.separator;
        file = new File(
                path,
                "log_"+fileDateFormater.format(System.currentTimeMillis())+".csv");


        // Write Headings
        StringBuilder line = new StringBuilder();
        for(String header : HEADINGS){
            line.append(header).append(DELIMITER);
        }

        file.delete();
        file.getParentFile().mkdirs();
        writeLine(line.toString());
    }


    public void write(String behaviour, double down, double up){
        StringBuilder line = new StringBuilder();
        line.append(getTime()).append(DELIMITER);
        line.append(behaviour).append(DELIMITER);
        line.append(down).append(DELIMITER);
        line.append(up).append(DELIMITER);
        line.append(getRat(context)).append(DELIMITER);
        writeLine(line.toString());
    }

    protected void writeLine(String line){
        try{
            PrintWriter out = new PrintWriter(new FileOutputStream(file, true));
            out.println(line);
            out.close();
        } catch(IOException e){
            log.warn(null, e);
        }
    }

    public static String getRat(Context context){
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();

        //WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        //WifiInfo wifiInfo = wm.getConnectionInfo();

        if(netInfo.getType() == ConnectivityManager.TYPE_MOBILE){
            TelephonyManager telMan = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            switch(telMan.getNetworkType()){
                case TelephonyManager.NETWORK_TYPE_GPRS:   return "GPRS";
                case TelephonyManager.NETWORK_TYPE_EDGE:   return "EDGE";
                case TelephonyManager.NETWORK_TYPE_UMTS:   return "UMTS";
                case TelephonyManager.NETWORK_TYPE_HSPA:
                case TelephonyManager.NETWORK_TYPE_HSDPA:
                case TelephonyManager.NETWORK_TYPE_HSUPA:  return "HSPA";
                case TelephonyManager.NETWORK_TYPE_HSPAP:  return "HSPAP";
                case TelephonyManager.NETWORK_TYPE_LTE:    return "LTE";
            }
        }
        return netInfo.getTypeName();
    }

    protected String getTime(){
        return dateFormater.format(System.currentTimeMillis());
    }
}
