package de.jg_cody.Teraplex.ui.Flur;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;

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

import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;
import java.util.Base64;
import java.util.Objects;

import de.jg_cody.Teraplex.AddButtonDialogSingle;
import de.jg_cody.Teraplex.AddButtonsDialogDouble;
import de.jg_cody.Teraplex.Cricketer;
import de.jg_cody.Teraplex.MainActivity;
import de.jg_cody.Teraplex.R;
import de.jg_cody.Teraplex.ui.rooms.*;

public class FlurFragment extends Fragment implements AddButtonDialogSingle.AddButtonDialogListenerSingle, AddButtonsDialogDouble.AddButtonsDialogListenerDouble {

    private FlurViewModel flurViewModel;


    String[] commands = {"Hello1","Hello2","Hello3","Hello4","Hello5"};
    int[] type = {0,1,0,0,1};

    ListView listView;
    ListAdapter listAdapter;

    ArrayList<ListItem> items;

    ArrayList<Cricketer> cricketersList = new ArrayList<>();

    int list_item_type;

    @RequiresApi(api = Build.VERSION_CODES.O)
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        flurViewModel =
                new ViewModelProvider(this).get(FlurViewModel.class);
        View root = inflater.inflate(R.layout.fragment_flur, 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);
        }



        listView = root.findViewById(R.id.listView);

        items = new ArrayList<ListItem>();



        listAdapter= new ListAdapter(getContext(), R.id.text, items);
        listView.setAdapter(listAdapter);

        FloatingActionButton addfab = root.findViewById(R.id.addbutton_fab);
        addfab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showChooseElement();
            }
        });




        return root;
    }
    private void showChooseElement() {
        final String[] listItems = {"SINGLE BUTTON", "DOUBLE BUTTON", "ÜBERSCHRIFT", "PLATZHALTER"};
        AlertDialog.Builder mBuilder = new AlertDialog.Builder(getActivity());
        mBuilder.setTitle("CHOOSE ELEMENT");
        mBuilder.setSingleChoiceItems(listItems, -5, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                if (i == 0) {
                    openDialog1();
                } else if (i == 1) {
                    openDialog2();
                } else if (i == 2) {

                } else if (i == 3) {
                    listAdapter.add(new ListItemSpace(3));
                    listAdapter.notifyDataSetChanged();

                }

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

    public void openDialog1() {
        list_item_type = 0;
        AddButtonDialogSingle addButtonDialogSingle = new AddButtonDialogSingle();
        addButtonDialogSingle.setFragment(this);
        addButtonDialogSingle.show(getActivity().getSupportFragmentManager(), "example dialog");

    }

    public void openDialog2() {
        list_item_type = 1;
        AddButtonsDialogDouble addButtonsDialogDouble = new AddButtonsDialogDouble();
        addButtonsDialogDouble.setFragment(this);
        addButtonsDialogDouble.show(getActivity().getSupportFragmentManager(), "example dialog");
    }

    @Override
    public void applyTextsSingle(String name, String command, String button) {
        //items.add(new ListItemSingle(0,name,command,button));
        listAdapter.add(new ListItemSingle(list_item_type,name,command,button));
        listAdapter.notifyDataSetChanged();

    }

    @Override
    public void applyTextsDouble(String name, String command1, String command2, String button1, String button2) {
        listAdapter.add(new ListItemDouble(list_item_type,name, command1, command2, button1, button2));
        listAdapter.notifyDataSetChanged();
    }
}