package de.tudarmstadt.informatik.hostage.ui2.fragment;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import de.tudarmstadt.informatik.hostage.R;
import de.tudarmstadt.informatik.hostage.logging.Record;
import de.tudarmstadt.informatik.hostage.logging.UglyDbHelper;

/**
 * Created by Julien on 02.03.14.
 */
public class RecordDetailFragment extends Fragment {
    private class Location{
        private double longitude;
        private double latitude;
        private float accuracy;

        public double getLatitude() {
            return latitude;
        }
        public void setLatitude(double latitude) {
            this.latitude = latitude;
        }
        public float getAccuracy() {
            return accuracy;
        }

		public void setAccuracy(float accuracy) {
			this.accuracy = accuracy;
		}

		public double getLongitude(){
            return this.longitude;
        }
        public void setLongitude(double longitude){
            this.longitude = longitude;
        }

        public Location(){
            super();
        }
        public Location(double latitude, double longitude, float accuracy){
            super();
            this.latitude = latitude;
            this.longitude = longitude;
            this.accuracy = accuracy;
        }
    }

    private Record record;
    private UglyDbHelper dbh;
    private View rootView;

    public void setRecord(Record rec){
        this.record = rec;
    }
    public Record getRecord(){
        return this.record;
    }

    public int getLayoutId(){
        return R.layout.fragment_record_detail;
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);

        this.dbh = new UglyDbHelper(this.getActivity().getBaseContext());

        this.rootView = inflater.inflate(this.getLayoutId(), container, false);
        this.configurateRootView(rootView);


        return rootView;
    }

    public void onStart() {
        super.onStart();

    }

    private void configurateRootView(View rootView) {
		/*
        String protocol = this.record.getProtocol();
        String type = "" + this.record.getType();
        String externalIP = this.record.getExternalIP();
        String localIP = this.record.getLocalIP();
        String localPort = "" + this.record.getLocalPort();
        String remoteIP = this.record.getRemoteIP();
        String bssid = this.record.getBssid() ;
        String essid = this.record.getSsid();
        double latitude = this.record.getLatitude();
        double longitude = this.record.getLongitude();
        float accuracy =this.record.getAccuracy();

        Location  location = new Location(latitude, longitude, accuracy);

        String packet = this.record.getPacket();
        String timeStamp = this.getDateAsString(this.record.getTimestamp());
        */

		((TextView)rootView.findViewById(R.id.record_detail_ssid_value)).setText(record.getSsid());
		((TextView)rootView.findViewById(R.id.record_detail_bssid_value)).setText(record.getBssid());
		((TextView)rootView.findViewById(R.id.record_detail_protocol_value)).setText(record.getProtocol());
		((TextView)rootView.findViewById(R.id.record_detail_timestamp_value)).setText(getDateAsString(record.getTimestamp()));
		((TextView)rootView.findViewById(R.id.record_detail_location_value)).setText(record.getLatitude() + ", " + record.getLongitude());
		((TextView)rootView.findViewById(R.id.record_detail_externalip_value)).setText(record.getExternalIP());
		((TextView)rootView.findViewById(R.id.record_detail_internalip_value)).setText(record.getLocalIP() + ":" + record.getLocalPort());
		((TextView)rootView.findViewById(R.id.record_detail_remoteip_value)).setText(record.getRemoteIP() + ":" + record.getRemotePort());
		((TextView)rootView.findViewById(R.id.record_detail_packettype_value)).setText(
				record.getType() == Record.TYPE.SEND ? "SEND" : "RECV");
		((TextView)rootView.findViewById(R.id.record_detail_packetcontent_value)).setText(record.getPacket());
    }


    /*****************************
     *
     *          Date Transform
     *
     * ***************************/


    @SuppressLint("SimpleDateFormat")
    private String getDateAsString(long timeStamp) {

        try {
            DateFormat sdf = new SimpleDateFormat("H:mm d.M.yy");
            Date netDate = (new Date(timeStamp));
            return sdf.format(netDate);
        } catch (Exception ex) {
            return "xx";
        }
    }


    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
    }






}
