package cpm.com.gskmtorange;

import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class SplashScreenActivity extends AppCompatActivity {

    private static int SPLASH_TIME_OUT = 3000;
    LinearLayout linearLayout;

    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        Window window = getWindow();
        window.setFormat(PixelFormat.RGBA_8888);
    }
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_main_layout);

        linearLayout = findViewById(R.id.lin_lay);

        StartAnimations();

        sendToLogin();

       /* File f = new File(CommonString.FILE_PATH_OLD);
        if(f!=null){
            File file[] = f.listFiles();
            if(file!=null){

                for (int i=0;i<file.length;i++){
                    Date lastModDate = new Date(file[0].lastModified());
                    String day = lastModDate.toString();

                *//*SimpleDateFormat spf= new SimpleDateFormat("MM/dd/yyyy");
                date = spf.format(newDate);
                System.out.println(date);*//*
                }
               *//* if(file.length>0){
                    UploadImageWithRetrofit.uploadedFiles = 0;
                    UploadImageWithRetrofit.totalFiles = file.length;
                    UploadImageWithRetrofit uploadImg = new UploadImageWithRetrofit( SplashScreenActivity.this);
                    uploadImg.UploadImageRecursive(SplashScreenActivity.this);
                }
                else {
                    sendToLogin();
                }*//*
            }else {
                sendToLogin();
            }

        }
        else {
            sendToLogin();
        }
*/

    }
    private void StartAnimations() {
        Animation anim = AnimationUtils.loadAnimation(this, R.anim.alpha);
        anim.reset();
        LinearLayout l=(LinearLayout) findViewById(R.id.lin_lay);
        l.clearAnimation();
        l.startAnimation(anim);

        anim = AnimationUtils.loadAnimation(this, R.anim.translate);
        anim.reset();
        ImageView iv = (ImageView) findViewById(R.id.logo);
        iv.clearAnimation();
        iv.startAnimation(anim);

    }

    public void sendToLogin(){
        new Handler().postDelayed(new Runnable() {

			/*
			 * Showing splash screen with a timer. This will be useful when you
			 * want to show case your app logo / company
			 */

            @Override
            public void run() {
                // This method will be executed once the timer is over
                // Start your app main activity

               /* SharedPreferences sharedpreferences = getSharedPreferences("MyPref", Context.MODE_PRIVATE);

                boolean isLoggedIn = sharedpreferences.getBoolean("isLoggedIn", false);

                if (isLoggedIn) {

                    Intent i = new Intent(SplashScreenActivity.this, LoginActivity.class);
                    startActivity(i);
                } else {

                    Intent i = new Intent(SplashScreenActivity.this, LoginActivity.class);
                    startActivity(i);

                }*/

                // close this activity
                Intent i = new Intent(SplashScreenActivity.this, LoginActivity.class);
                startActivity(i);

                overridePendingTransition(R.anim.activity_in, R.anim.activity_out);

                finish();
            }
        }, SPLASH_TIME_OUT);
    }

    public static String getCalculatedDate(String date, String dateFormat, int days) {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat s = new SimpleDateFormat(dateFormat);
        cal.add(Calendar.DAY_OF_YEAR, days);
        try {
            return s.format(new Date(s.parse(date).getTime()));
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            Log.e("TAG", "Error in Parsing Date : " + e.getMessage());
        }
        return null;
    }

   /* public boolean isValidDateForUpload(String dateImg){
        boolean flag=false;
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        Date strDate = sdf.parse(valid_until);
        if (System.currentTimeMillis() > strDate.getTime()) {
            flag = true;
        }

        return flag;
    }*/
}
