package mercuryretrograde.findyourfate.com;

import java.text.SimpleDateFormat; 
import java.util.Date;
import java.util.List; 
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.ParseException;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Html;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class mercurycountdown extends Activity {
    /** Called when the activity is first created. */
    TextView tv;
    long diff;
    long milliseconds;
    long endTime;
    final Context context = this;   
    protected SQLiteDatabase DB;
	protected Cursor cursor; 
	String dateString="";
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        tv = new TextView(this);
        this.setContentView(tv);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HH:mm");
        formatter.setLenient(false);
        DatabaseHandler db = new DatabaseHandler(this);
	    
   	 
        List<Contact> contacts =db.getAllContacts();    
        for (Contact cn : contacts) {  
				 
					int nextsno=cn.getupSno();
					DB = new DatabaseHandler(context).getReadableDatabase();
					  Cursor cursor = DB.rawQuery("SELECT * FROM findmercury WHERE sno='"+nextsno+"'", null);   
					  
					   
					  
					  
					if (cursor.moveToFirst())
				        {
				      do {
				    	    
				    	     dateString=cursor.getString(1);
				    	   //  y=dateString.substring(0,4); 
				    	    // mox=dateString.substring(4,6);
				    	    // d=dateString.substring(6,8); 
				    	     
				    			 
				      }	while (cursor.moveToNext());
				      cursor.close(); 
				      } 
					
					  
					 
				 
        } 
        
 
		 Toast.makeText(context,"dateString="+dateString,Toast.LENGTH_LONG).show();
		
        // String oldTime = "20170409 12:00";
		
		
	 String oldTime =dateString;
        Date oldDate;
        try {
            oldDate = formatter.parse(oldTime);
             milliseconds = oldDate.getTime();

            //long startTime = System.currentTimeMillis();
            // do your work...
          /*  long endTime=System.currentTimeMillis();

             diff = milliseconds-endTime;       

           // Log.e("day", "miliday"+diff);
            long seconds = (long) (diff / 1000) % 60 ;
            //Log.e("secnd", "miliday"+seconds);
            long minutes = (long) ((diff / (1000*60)) % 60);
           // Log.e("minute", "miliday"+minutes);
            long hours   = (long) ((diff / (1000*60*60)) % 24);
           // Log.e("hour", "miliday"+hours);
            long days = (int)((diff / (1000*60*60*24)) % 365);
           // Log.e("days", "miliday"+days);*/
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (java.text.ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


        Long serverUptimeSeconds = (milliseconds-System.currentTimeMillis()) / 1000;


            String serverUptimeText = String.format("%d days %d hours %d minutes %d seconds",
            serverUptimeSeconds / 86400,
            ( serverUptimeSeconds % 86400) / 3600 ,
            ((serverUptimeSeconds % 86400) % 3600 ) / 60,
            ((serverUptimeSeconds % 86400) % 3600 ) % 60
            );


        Log.v("jjj", "miliday"+serverUptimeText);
        MyCount counter = new MyCount(milliseconds,1000);
        counter.start();


    }


    // countdowntimer is an abstract class, so extend it and fill in methods
    public class MyCount extends CountDownTimer {
        public MyCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {
            tv.setText("done!");
        }

        @Override
        public void onTick(long millisUntilFinished) {
            //tv.setText("Left: " + millisUntilFinished / 1000);

           /* long diff = endTime - millisUntilFinished; 
           // Log.e("left", "miliday"+diff);
            long seconds = (long) (diff / 1000) % 60 ;
            //Log.e("secnd", "miliday"+seconds);
            long minutes = (long) ((diff / (1000*60)) % 60);
            //Log.e("minute", "miliday"+minutes);
            long hours   = (long) ((diff / (1000*60*60)) % 24);
            //Log.e("hour", "miliday"+hours);
            int days = (int)((diff / (1000*60*60*24)) % 365);
           // Log.v("days", "miliday"+days);*/
            Long serverUptimeSeconds =  (millisUntilFinished-System.currentTimeMillis()) / 1000;

  
                String serverUptimeText = 
                String.format("%d "+"\nDays\n %d Hrs\n %d Mts\n %d Secs",
                serverUptimeSeconds / 86400,
                ( serverUptimeSeconds % 86400) / 3600 ,
                ((serverUptimeSeconds % 86400) % 3600 ) / 60,
                ((serverUptimeSeconds % 86400) % 3600 ) % 60
                );  

                Log.v("new its", "miliday"+serverUptimeText);

             // tv.setText(days +":"+hours+":"+minutes + ":" + seconds);

                tv.setText(Html.fromHtml("<h3 align='center'>Next Mercury Retrograde in :</h3><div align='center'>"+serverUptimeText+"</div>"));
        }
    }
}