package com.saptneel.themeselector;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.appcompat.widget.SwitchCompat;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;

import com.saptneel.themeselector.databinding.ActivityStylesAndThemeBinding;

import java.util.Objects;

//This activity is the one that you can copy into your app to have multiple color schemes.
public class StylesAndTheme extends AppCompatActivity {

    SharedPreferences sharedPreferences;
    ActivityStylesAndThemeBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //Getting system default theme.
        int theme = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;

        /*Creating a shared preferences variable to save the user theme,
         which by default is set same as system default.*/
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        boolean isNight = sharedPreferences.getBoolean("NightMode", theme == Configuration.UI_MODE_NIGHT_YES);
        /*There is no need to apply the night theme in this activity as it is already set in Main
         Activity. We just need shared preferences for our switch in the activity.*/

        //Applying the style saved by the user, which by default is also same as android default.
        int styleId = sharedPreferences.getInt("Style", R.style.Theme_ThemeSelector);
        setTheme(styleId);

        binding = ActivityStylesAndThemeBinding.inflate(getLayoutInflater());
        Objects.requireNonNull(getSupportActionBar()).setTitle("Styles and Theme");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        super.onCreate(savedInstanceState);
        setContentView(binding.getRoot());

        //Setting up Dark Mode Switch and its listener;
        binding.darkSwitch.setChecked(isNight);
        binding.darkSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if(isChecked) {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                sharedPreferences.edit().putBoolean("NightMode", true).apply();
            }
            else {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                sharedPreferences.edit().putBoolean("NightMode", false).apply();
            }

        });

    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void applyStyle1(View view) {
        sharedPreferences.edit().putInt("Style", R.style.Theme_ThemeSelector_Style1).apply();
        finish();
        startActivity(getIntent());
    }

    public void applyStyle2(View view) {
        sharedPreferences.edit().putInt("Style", R.style.Theme_ThemeSelector_Style2).apply();
        finish();
        startActivity(getIntent());
    }

    public void applyStyle3(View view) {
        sharedPreferences.edit().putInt("Style", R.style.Theme_ThemeSelector_Style3).apply();
        finish();
        startActivity(getIntent());
    }

    public void applyStyle4(View view) {
        sharedPreferences.edit().putInt("Style", R.style.Theme_ThemeSelector_Style4).apply();
        finish();
        startActivity(getIntent());
    }

    public void applyStyle5(View view) {
        sharedPreferences.edit().putInt("Style", R.style.Theme_ThemeSelector_Style5).apply();
        finish();
        startActivity(getIntent());
    }

    public void applyStyle6(View view) {
        sharedPreferences.edit().putInt("Style", R.style.Theme_ThemeSelector_Style6).apply();
        finish();
        startActivity(getIntent());
    }

    public void applyStyle7(View view) {
        sharedPreferences.edit().putInt("Style", R.style.Theme_ThemeSelector_Style7).apply();
        finish();
        startActivity(getIntent());
    }

    public void applyStyle8(View view) {
        sharedPreferences.edit().putInt("Style", R.style.Theme_ThemeSelector_Style8).apply();
        finish();
        startActivity(getIntent());
    }

    public void applyStyle9(View view) {
        sharedPreferences.edit().putInt("Style", R.style.Theme_ThemeSelector_Style9).apply();
        finish();
        startActivity(getIntent());
    }

    public void applyStyle10(View view) {
        sharedPreferences.edit().putInt("Style", R.style.Theme_ThemeSelector_Style10).apply();
        finish();
        startActivity(getIntent());
    }

    public void applyStyleDefault(View view) {
        sharedPreferences.edit().putInt("Style", R.style.Theme_ThemeSelector).apply();
        finish();
        startActivity(getIntent());
    }
}