package de.jg_cody.Teraplex.ui.Einstellungen;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.rarepebble.colorpicker.ColorPickerView;

import java.util.Base64;
import java.util.Locale;
import java.util.Objects;

import de.jg_cody.Teraplex.MainActivity;
import de.jg_cody.Teraplex.R;

import static android.content.Context.MODE_PRIVATE;

public class EinstellungenFragment extends Fragment {

    private EinstellungenViewModel einstellungenViewModel;

    @RequiresApi(api = Build.VERSION_CODES.O)
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        einstellungenViewModel =
                new ViewModelProvider(this).get(EinstellungenViewModel.class);
        View root = inflater.inflate(R.layout.fragment_einstellungen, container, false);
        SharedPreferences p = requireContext().getSharedPreferences("appsettings", Activity.MODE_PRIVATE);
        String Background = p.getString("Background", null);
        if (Background != null) {
            ImageView I = root.findViewById(R.id.Background);
            byte[] BA = Base64.getDecoder().decode(Background);
            I.setImageDrawable(new BitmapDrawable(getResources(), MainActivity.scaleCenterCrop(BitmapFactory.decodeByteArray(BA, 0, BA.length), MainActivity.getScreenHeight(), MainActivity.getScreenWidth())));
            I.setScaleType(ImageView.ScaleType.CENTER_CROP);
        }
        Button Language = root.findViewById(R.id.button_language);
        Language.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showChangeLanguageDialog();

            }
        });
        Button Reset = root.findViewById(R.id.button_reset);
        Reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MediaPlayer mp = MediaPlayer.create(getContext(), R.raw.alert_error);
                mp.start();
                AlertDialog mDialog = new AlertDialog.Builder(getContext())
                        .setTitle(R.string.reset_app)
                        .setMessage(R.string.are_you_sure_to_reset)

                        // Specifying a listener allows you to take an action before dismissing the dialog.
                        // The dialog is automatically dismissed when a dialog button is clicked.
                        .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                try {
                                    requireContext().getSharedPreferences("appsettings", Context.MODE_PRIVATE).edit().clear().apply();
                                    startActivity(requireActivity().getIntent());
                                    requireActivity().finish();
                                } catch (Exception ignored) {
                                }

                                // Continue with delete operation
                            }
                        })

                        // A null listener allows the button to dismiss the dialog and take no further action.
                        .setNegativeButton(android.R.string.no, null)
                        .create();
                Objects.requireNonNull(mDialog.getWindow()).setBackgroundDrawableResource(R.drawable.button_round);
                mDialog.show();
            }

        });
        Button choose_background = root.findViewById(R.id.button_pickimage);
        choose_background.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showChangeBackgroundDialog();


            }
        });
        Button theme1 = root.findViewById(R.id.button_theme_red_blue);
        theme1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences.Editor editor = requireContext().getSharedPreferences("appsettings", MODE_PRIVATE).edit();
                editor.putString("Theme", "RED_BLUE");
                editor.apply();
                startActivity(requireActivity().getIntent());
                requireActivity().finish();


            }
        });
        Button theme2 = root.findViewById(R.id.button_theme_green_yellow);
        theme2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences.Editor editor = requireContext().getSharedPreferences("appsettings", MODE_PRIVATE).edit();
                editor.putString("Theme", "GREEN_YELLOW");
                editor.apply();
                startActivity(requireActivity().getIntent());
                requireActivity().finish();


            }
        });
        Button theme3 = root.findViewById(R.id.button_theme_orange_turquoise);
        theme3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences.Editor editor = requireContext().getSharedPreferences("appsettings", MODE_PRIVATE).edit();
                editor.putString("Theme", "ORANGE_TURQUOISE");
                editor.apply();
                startActivity(requireActivity().getIntent());
                requireActivity().finish();


            }
        });
        Button theme4 = root.findViewById(R.id.button_theme_yellow_blue);
        theme4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences.Editor editor = requireContext().getSharedPreferences("appsettings", MODE_PRIVATE).edit();
                editor.putString("Theme", "YELLOW_BLUE");
                editor.apply();
                startActivity(requireActivity().getIntent());
                requireActivity().finish();


            }
        });
        Button theme5 = root.findViewById(R.id.button_theme_pink_green);
        theme5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences.Editor editor = requireContext().getSharedPreferences("appsettings", MODE_PRIVATE).edit();
                editor.putString("Theme", "PINK_GREEN");
                editor.apply();
                startActivity(requireActivity().getIntent());
                requireActivity().finish();


            }
        });

        return root;
    }

    private void showChangeLanguageDialog() {
        final String[] listItems = {"GERMAN", "ENGLISCH", "KLINGONISCH"};
        AlertDialog.Builder mBuilder = new AlertDialog.Builder(getContext());
        mBuilder.setTitle("CHOOSE LANGUAGE");
        mBuilder.setSingleChoiceItems(listItems, -5, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                if (i == 0) {
                    setLocale("de");
                    startActivity(requireActivity().getIntent());
                    requireActivity().finish();
                } else if (i == 1) {
                    setLocale("en");
                    startActivity(requireActivity().getIntent());
                    requireActivity().finish();
                } else if (i == 2) {
                    setLocale("tlh");
                    startActivity(requireActivity().getIntent());
                    requireActivity().finish();
                }
                dialogInterface.dismiss();
            }
        });
        AlertDialog mDialog = mBuilder.create();
        Objects.requireNonNull(mDialog.getWindow()).setBackgroundDrawableResource(R.drawable.button_round);
        mDialog.show();
    }

    private void showChangeBackgroundDialog() {
        final String[] listItems = {"FARBE", "GRADIENT", "GRAFIK", "RESET BACKGROUND"};
        AlertDialog.Builder mBuilder = new AlertDialog.Builder(getContext());
        mBuilder.setTitle("CHOOSE BACKGROUND-TYPE");
        mBuilder.setSingleChoiceItems(listItems, -5, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                if (i == 0) {
                    try {
                        final ColorPickerView picker = new ColorPickerView(getContext());
                        picker.setColor(0xff12345);
                        AlertDialog mDialog = new AlertDialog.Builder(getContext()).setView(picker).setTitle(R.string.colorpicker).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        }).create();
                        Objects.requireNonNull(mDialog.getWindow()).setBackgroundDrawableResource(R.drawable.button_round);
                        mDialog.show();
                    } catch (Exception e) {
                    }
                } else if (i == 1) {
                    try {
                        final ColorPickerView picker = new ColorPickerView(getContext());
                        picker.setColor(0xff12345);
                        AlertDialog mDialog = new AlertDialog.Builder(getContext()).setView(picker).setTitle(R.string.colorpicker).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {


                            }
                        }).create();
                        Objects.requireNonNull(mDialog.getWindow()).setBackgroundDrawableResource(R.drawable.button_round);
                        mDialog.show();
                    } catch (Exception ignored) {
                    }
                } else if (i == 2) {
                    Intent a = new Intent(
                            Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    requireActivity().startActivityForResult(a, MainActivity.RESULT_LOAD_IMAGE);
                } else if (i == 3) {
                    requireContext().getSharedPreferences("appsettings", Context.MODE_PRIVATE).edit().remove("Background").apply();
                    startActivity(requireActivity().getIntent());
                    requireActivity().finish();

                }
                dialogInterface.dismiss();
            }
        });
        AlertDialog mDialog = mBuilder.create();
        Objects.requireNonNull(mDialog.getWindow()).setBackgroundDrawableResource(R.drawable.button_round);
        mDialog.show();
    }

    private void setLocale(String lang) {
        Locale locale = new Locale(lang);
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        requireContext().getResources().updateConfiguration(config, requireContext().getResources().getDisplayMetrics());
        SharedPreferences.Editor editor = requireContext().getSharedPreferences("appsettings", MODE_PRIVATE).edit();
        editor.putString("Language", lang);
        editor.apply();
    }



}