package com.project.ilsaenterprise;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.firebase.auth.FirebaseAuth;

import java.io.File;
import java.util.Calendar;

public class LoginBatal extends AppCompatActivity {
    private TextView year;
    private RelativeLayout btnLogout;
    SharedPref sharedPreference;
    private FirebaseAuth mAuth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.Dark));
        setContentView(R.layout.activity_login_batal);
        getSupportActionBar().hide();

        Calendar c = Calendar.getInstance();
        int getYear = c.get(Calendar.YEAR);
        sharedPreference = new SharedPref(this);
        mAuth = FirebaseAuth.getInstance();

        btnLogout = findViewById(R.id.keluarBtn);
        year = findViewById(R.id.yearBottom);
        year.setText(String.valueOf(getYear));

        btnLogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mAuth.signOut();
                deleteCache(LoginBatal.this);
                //sharedPreference.saveSPBoolean(SharedPreference.SP_SUDAH_LOGIN, false);
                startActivity(new Intent(LoginBatal.this, Login.class)
                        .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK));
            }
        });
    }

    public static void deleteCache(Context context) {
        try {
            File dir = context.getCacheDir();
            deleteDir(dir);
        } catch (Exception e) { e.printStackTrace();}
    }

    public static boolean deleteDir(File dir) {
        if (dir != null && dir.isDirectory()) {
            String[] children = dir.list();
            int i = 0;
            while (i < children.length) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
                i++;
            }
        }

        assert dir != null;
        return dir.delete();
    }
}