package com.kfb.kfbv1.shttp;

import android.os.Environment;
import android.util.Log;

import com.kfb.kfbv1.db.DbManage;
import com.kfb.kfbv1.db.Pomiar;
import com.kfb.kfbv1.model.KfbModel;


import org.nanohttpd.protocols.http.IHTTPSession;
import org.nanohttpd.protocols.http.NanoHTTPD;
import org.nanohttpd.protocols.http.response.IStatus;
import org.nanohttpd.protocols.http.response.Response;
import org.nanohttpd.protocols.http.response.Status;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;




public class KfbHttp extends NanoHTTPD {
    public static final String  MIME_PNG = "image/png";
    private static final int PORT = 9999;
    public KfbHttp() throws IOException {
        super(PORT);
        start();
    }
    @Override
    public Response serve(IHTTPSession session) {
String uri = session.getUri();
String html ="";
        if(uri.equals("/administracja")==true)
        {
            try {
                // Opening file from SD Card
                File root = Environment.getExternalStorageDirectory();
                FileReader index = new FileReader(root.getAbsolutePath() +
                        "/DCIM/www/index.html");
                BufferedReader reader = new BufferedReader(index);
                String line = "";
                while ((line = reader.readLine()) != null) {
                    html += line;
                }


            } catch(IOException ioe) {
                Log.w("Httpd", ioe.toString());
            }
        }else
        {
            if(uri.equals("/admin.html")==true)
            {
                String pas="";
                String log="";
               boolean isLogin= session.getParameters().containsKey("login");
               if(isLogin==true)
               {
                    log = session.getParameters().get("login").get(0);
               }
               boolean isPass = session.getParameters().containsKey("password");
                if(isPass==true)
                {
                     pas = session.getParameters().get("password").get(0);
                }

                if(log.equals("admin")==true && pas.equals("admin")==true)
                {

                   try {
                        // Opening file from SD Card
                        File root = Environment.getExternalStorageDirectory();
                        FileReader index = new FileReader(root.getAbsolutePath() +
                                "/DCIM/www/"+uri);
                        BufferedReader reader = new BufferedReader(index);
                        String line = "";
                        while ((line = reader.readLine()) != null) {
                            html += line;
                        }


                    } catch(IOException ioe) {
                        Log.w("Httpd", ioe.toString());
                    }
                    return Response.newFixedLengthResponse(html);
                }
                else
                {
                    try {
                        // Opening file from SD Card
                        File root = Environment.getExternalStorageDirectory();
                        FileReader index = new FileReader(root.getAbsolutePath() +
                                "/DCIM/www/index.html");
                        BufferedReader reader = new BufferedReader(index);
                        String line = "";
                        while ((line = reader.readLine()) != null) {
                            html += line;
                        }


                    } catch(IOException ioe) {
                        Log.w("Httpd", ioe.toString());
                    }
                    return Response.newFixedLengthResponse(html);
                }
            }else {
                try {
                    if (uri.contains(".png") == true) {

                        File root = Environment.getExternalStorageDirectory();
                        //FileReader index = new FileReader(root.getAbsolutePath() +
                        //        "/DCIM/www" + uri);

                        FileInputStream fis = new FileInputStream(root.getAbsolutePath() +
                                "/DCIM/www" + uri);
                        //declaration of MIME_PNG in NanoHTTPD Core
                        long fileSize = new File(root.getAbsolutePath() +
                                "/DCIM/www" + uri).length();

                        byte[] allBytes = new byte[(int) fileSize];

                        fis.read(allBytes);

                        return Response.newFixedLengthResponse(Status.OK, MIME_PNG, allBytes);

                    } else {
                        // Opening file from SD Card
                        File root = Environment.getExternalStorageDirectory();
                        FileReader index = new FileReader(root.getAbsolutePath() +
                                "/DCIM/www" + uri);
                        BufferedReader reader = new BufferedReader(index);
                        String line = "";
                        while ((line = reader.readLine()) != null) {
                            html += line;
                        }
                    }

                } catch (IOException ioe) {
                    Log.w("Httpd", ioe.toString());
                }
            }
        }

if(uri.equals("/kfb")==true)
{
    DbManage dbRep = new DbManage(KfbModel.getData().context);
    List<Pomiar> lp = dbRep.getPomiarAll();

    html = "<!DOCTYPE HTML> <html><style> body { min-width: 300px; margin: auto; font-family: Helvetica, Tahoma;  font-size: 14pt;  font-style: normal;  font-variant: normal; background:#fff } a {text-decoration: none;color:#0170C0  } div { display: table; margin-right: auto;   margin-left: auto; padding:2px; } h3 { font-family: Helvetica, Tahoma, Verdana; font-size: 18pt; font-weight: normal;color:#000; } h4 { font-family: Helvetica, Tahoma, Verdana; font-size: 16pt; font-weight: normal;color:#848484; text-align:center; } td {text-align:left ;padding:10px; border: 2px solid #848484;  padding:12px; font-size: 14px; } tr,th {padding:13px 10px 10px 10px; font-size:12pt;font-weight:normal} </style> <BODY> <table style=\"margin: 30px;\"><TR><TH>Bramka</TH><TH>Data</TH><TH>Temperatura</TH><TH>Składowe</TH></TR>";
    for(int i = 0;i< lp.size();i++)
    {
            html = html + "<TR><TD>" + lp.get(i).gate + "</TD><TD>" + lp.get(i).datep + "</TD><TD>" + lp.get(i).temperatura + "</TD><TD>" + lp.get(i).skladowe + "</TD></TR>";
    }
    html= html+"</table></BODY></html>";

  //  html = "<!DOCTYPE HTML> <html><table style=\"border-width: 1px; border-style: solid; border-color: black\"><TR><TH>Bramka</TH><TH>Data</TH><TH>Temperatura</TH><TH>Składowe</TH></TR>";
  //  for(int i = 0;i< lp.size();i++)
  //  {
  //      html= html+"<TR><TD>"+lp.get(i).gate+"</TD><TD>"+lp.get(i).datep+"</TD><TD>"+lp.get(i).temperatura+"</TD><TD>"+lp.get(i).skladowe+"</TD></TR>";
  //  }
  //  html= html+"</table></html>";
}


        return Response.newFixedLengthResponse(html);
    }
}
