package org.micromanager;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import mmcorej.CMMCore;
import mmcorej.StrVector;
import org.micromanager.utils.HttpUtils;
import org.micromanager.utils.MMUUEncoder;
import org.micromanager.utils.ReportingUtils;

class ProblemReportSender extends Thread {

    private String status_;
    private String currentCfgPath_;
    private CMMCore core_;
    String prepreamble_;

    public ProblemReportSender(String prepreamble, CMMCore c, String config) {
        super("sender");
        status_ = "";
        currentCfgPath_ = config;
        core_ = c;
        prepreamble_ = prepreamble;
    }

    public String Status() {
        return status_;
    }

    public void run() {
        status_ = "";
        // a handy, unique ID for the equipment
        String physicalAddress = "00-00-00-00-00-00";
        String cfgFile = currentCfgPath_;
        // is there a public way to get these keys??
        //mainPrefs_.get("sysconfig_file", cfgFile);
        String preamble = prepreamble_;
        if(0< preamble.length())
            preamble += "\n";
        preamble += "#";
        StrVector ss = core_.getMACAddresses();
        if (0 < ss.size()){
            String pa2 = ss.get(0);
            if(null != pa2){
                if( 0 <  pa2.length()){
                    physicalAddress = pa2;
                }
            }
        }
        preamble += ("MAC: " + physicalAddress + " ");
        try {

            preamble += "\n#Host: " + InetAddress.getLocalHost().getHostName() + " ";
        } catch (IOException e) {
        }
        preamble += ("\n#User: " + core_.getUserId() + "\n#configuration file: " + cfgFile + "\n");
        try {
            Reader in = new BufferedReader(new FileReader(cfgFile));
            StringBuilder sb = new StringBuilder();
            char[] tmpBuffer = new char[8192];
            int length;

            while ((length = in.read(tmpBuffer)) > 0) {
                sb.append(tmpBuffer, 0, length);
            }
            preamble += sb.toString();
            preamble += "\n";
        } catch (IOException e) {
        }
        String archPath = core_.saveLogArchiveWithPreamble(preamble, preamble.length());
        //String archPath = core_.saveLogArchive();
        try {
            HttpUtils httpu = new HttpUtils();
            List<File> list = new ArrayList<File>();
            File archiveFile = new File(archPath);
            // contruct a filename for the archive which is extremely
            // likely to be unique as follows:
            // yyyyMMddHHmmss + timezone + ip address
            String qualifiedArchiveFileName = "";
            try {
                SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
                qualifiedArchiveFileName += df.format(new Date());
                String shortTZName = TimeZone.getDefault().getDisplayName(false, TimeZone.SHORT);
                qualifiedArchiveFileName += shortTZName;
                qualifiedArchiveFileName += "_";
                qualifiedArchiveFileName += physicalAddress; //InetAddress.getLocalHost().getHostAddress();
            } catch (Throwable t) {
            }
            // try ensure valid and convenient UNIX file name
            qualifiedArchiveFileName.replace(' ', '_');
            qualifiedArchiveFileName.replace('*', '_');
            qualifiedArchiveFileName.replace('|', '_');
            qualifiedArchiveFileName.replace('>', '_');
            qualifiedArchiveFileName.replace('<', '_');
            qualifiedArchiveFileName.replace('(', '_');
            qualifiedArchiveFileName.replace(')', '_');
            qualifiedArchiveFileName.replace(':', '_');
            qualifiedArchiveFileName.replace(';', '_');                //File fileToSend = new File(qualifiedArchiveFileName);
            qualifiedArchiveFileName += ".log";
            MMUUEncoder uuec = new MMUUEncoder();
            InputStream reader = new FileInputStream(archiveFile);
            OutputStream writer = new FileOutputStream(qualifiedArchiveFileName);
            uuec.encodeBuffer(reader, writer);
            reader.close();
            writer.close();
            File fileToSend = new File(qualifiedArchiveFileName);
            try {
                URL url = new URL("http://valelab.ucsf.edu/~MM/upload_corelog.php");
                List flist = new ArrayList<File>();
                flist.add(fileToSend);
                // for each of a colleciton of files to send...
                for (Object o0 : flist) {
                    File f0 = (File) o0;
                    try {
                        httpu.upload(url, f0);
                    } catch (java.net.UnknownHostException e2) {
                        status_ = e2.toString();//, " log archive upload");
                    } catch (IOException e2) {
                        status_ = e2.toString();
                    } catch (SecurityException e2) {
                        status_ = e2.toString();
                    } catch (Exception e2) {
                        status_ = e2.toString();
                    }
                }
            } catch (MalformedURLException e2) {
                status_ = e2.toString();
            }
            if( !fileToSend.delete())
                ReportingUtils.logMessage("Couldn't delete temporary file " + qualifiedArchiveFileName );
            if(!archiveFile.delete())
                ReportingUtils.logMessage("Couldn't delete archive file " + archPath);
        } catch (IOException e2) {
            status_ = e2.toString();
        }
    }
    public String Send(){
       start();
       try {
            join();
       } catch (InterruptedException ex) {
           status_ = ex.toString();

       }
       return Status();
    }
}



@@ -3016,18 +3016,19 @@
 ezone + 
-ip
+MAC
  address
@@ -3510,54 +3510,8 @@
 ss; 
-//InetAddress.getLocalHost().getHostAddress();
 %0A   
@@ -4355,24 +4355,179 @@
 chiveFile);%0A
+            // put the report in the tmp directory%0A            qualifiedArchiveFileName = System.getProperty(%22java.io.tmpdir%22) + qualifiedArchiveFileName;%0A
             

write problem report file in temp directory