package cpm.com.gskmtorange;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.appcompat.app.AppCompatActivity;

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

import cpm.com.gskmtorange.constant.CommonString;
import cpm.com.gskmtorange.password.MPinActivity;

public class SplashScreenActivity extends AppCompatActivity {

    private static int SPLASH_TIME_OUT = 3000;
    LinearLayout linearLayout;
    private Context context;
    private SharedPreferences preferences = null;

    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);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_splash_main_layout);

        linearLayout = findViewById(R.id.lin_lay);

        preferences = PreferenceManager.getDefaultSharedPreferences(this);
        context = this;
        //StartAnimations();

        sendToLogin();

       /* File f = new File(CommonString.getImagesFolder(context)_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() {
                //after Mpin
                String mpin = preferences.getString(CommonString.MPIN, null);
                if (mpin != null) {
                    Intent in = new Intent(context, MPinActivity.class);
                    in.putExtra(CommonString.IS_PASSWORD_CHECK, true);
                    startActivity(in);
                    finish();
                } else {
                    Intent i = new Intent(context, LoginActivity.class);
                    startActivity(i);
                    finish();
                }

            }
        }, SPLASH_TIME_OUT);
    }

    public static String getCalculatedDate(String date, String dateFormat, int days) {
        Calendar cal = Calendar.getInstance();
        @SuppressLint("SimpleDateFormat") 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;
    }

}
