package com.example.appli_calendar04;

import android.content.Intent;
import android.graphics.Color;
import android.net.ParseException;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
public class MainActivity extends AppCompatActivity 
{
    private static final String TAG = MainActivity.class.getSimpleName();
    private ImageView previousDay;
    private ImageView nextDay;
    private TextView currentDate;
    private Calendar cal = Calendar.getInstance();
    private DatabaseQuery mQuery;
    private RelativeLayout mLayout;
    private int eventIndex;
    
    
    Calendar cal_start = Calendar.getInstance();
    Calendar cal_end = Calendar.getInstance();
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //setContentView(R.layout.new_view);
        
        mQuery = new DatabaseQuery(this);
        mLayout = (RelativeLayout)findViewById(R.id.left_event_column);
        eventIndex = mLayout.getChildCount();
        currentDate = (TextView)findViewById(R.id.display_current_date);
        currentDate.setText(displayDateInString(cal.getTime()));
        displayDailyEvents();
        previousDay = (ImageView)findViewById(R.id.previous_day);
        nextDay = (ImageView)findViewById(R.id.next_day);
        
        
        previousDay.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                previousCalendarDate();
            }
        });
        
        
        nextDay.setOnClickListener(new View.OnClickListener() 
        {
            @Override
            public void onClick(View v)
            {
                nextCalendarDate();
            }
        });
    }
    
    private void previousCalendarDate()
    {
        mLayout.removeViewAt(eventIndex - 1);
        cal.add(Calendar.DAY_OF_MONTH, -1);
        currentDate.setText(displayDateInString(cal.getTime()));
        displayDailyEvents();
    }
    
    private void nextCalendarDate()
    {
        mLayout.removeViewAt(eventIndex - 1);
        cal.add(Calendar.DAY_OF_MONTH, 1);
        currentDate.setText(displayDateInString(cal.getTime()));
        displayDailyEvents();
    }
    
    private String displayDateInString(Date mDate)
    {
        SimpleDateFormat formatter = new SimpleDateFormat("d MMMM, yyyy", Locale.ENGLISH);
        return formatter.format(mDate);
    }
    
    private void displayDailyEvents()
    {
        Date calendarDate = cal.getTime();
        List<EventObjects> dailyEvent = mQuery.getAllFutureEvents(calendarDate);
        
        cal_start.set(2017,0,7,15,54);
        Date eventDate =  cal_start.getTime();
        
        cal_end.set(2017,0,7,18,02);
        Date endDate =  cal_end.getTime();
        
        String eventMessage = "Plouf";
        int eventBlockHeight = getEventTimeFrame(eventDate, endDate);
        Log.d(TAG, "Height " + eventBlockHeight);
        displayEventSection(eventDate, eventBlockHeight, eventMessage);

        		
        /*
        for(EventObjects eObject : dailyEvent)
        {
            Date eventDate = eObject.getDate();
            Date endDate = eObject.getEnd();
            String eventMessage = eObject.getMessage();
            int eventBlockHeight = getEventTimeFrame(eventDate, endDate);
            Log.d(TAG, "Height " + eventBlockHeight);
            displayEventSection(eventDate, eventBlockHeight, eventMessage);
        }
        */

    }
    
    private int getEventTimeFrame(Date start, Date end)
    {
        long timeDifference = end.getTime() - start.getTime();
        Calendar mCal = Calendar.getInstance();
        mCal.setTimeInMillis(timeDifference);
        
        long tmp = timeDifference / 60000;
        int nbre_minutes = (int) tmp;
        return nbre_minutes;
        
        //int hours = mCal.get(Calendar.HOUR);
        //int minutes = mCal.get(Calendar.MINUTE);
        //return (hours * 60) + ((minutes * 60) / 100);
    }
    
    
    private void displayEventSection(Date eventDate, int height, String message)
    {
        SimpleDateFormat timeFormatter = new SimpleDateFormat("HH:mm", Locale.ENGLISH);
        String displayValue = timeFormatter.format(eventDate);
        String[]hourMinutes = displayValue.split(":");
        int hours = Integer.parseInt(hourMinutes[0]);
        int minutes = Integer.parseInt(hourMinutes[1]);
        Log.d(TAG, "Hour value " + hours);
        Log.d(TAG, "Minutes value " + minutes);
        int topViewMargin = (hours * 60) +  minutes; //     ((minutes * 60) / 100);
        Log.d(TAG, "Margin top " + topViewMargin);
        createEventView(topViewMargin, height, message);
    }
    
    private void createEventView(int topMargin, int height, String message)
    {
        TextView mEventView = new TextView(MainActivity.this);
        RelativeLayout.LayoutParams lParam = new RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lParam.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        lParam.topMargin = topMargin * 2;
        lParam.leftMargin = 24;
        mEventView.setLayoutParams(lParam);
        mEventView.setPadding(24, 0, 24, 0);
        mEventView.setPadding(120, 0, 120, 0);
        
        //height = 50;
        mEventView.setHeight(height * 2);
        mEventView.setGravity(0x11);
        mEventView.setTextColor(Color.parseColor("#ffffff"));
        
        
        cal_start.set(2017,0,7,15,42,30);
        cal_end.set(2017,0,7,18,12,30);
        
        // 15H 900
        // 16H 960
        //15 * 60
        //16 * 60
        
        //8280001     
        
        // 1heure = 60s  * 60min * 1000
        //int minutes = (int) ((milliseconds / 1000) / 60);
        
        mEventView.setText(message);
        mEventView.setBackgroundColor(Color.parseColor("#3F51B5"));
        mLayout.addView(mEventView, eventIndex - 1);
        
        
        mEventView.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
            	Toast.makeText(getApplicationContext(), "msg msg", Toast.LENGTH_SHORT).show();
            	
            	Intent intent = new Intent(MainActivity.this, add_event.class);
                startActivity(intent);
            }
        });
        
    }
}