package de.jg_cody.Teraplex.ui.Flur;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
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.Objects;

import de.jg_cody.Teraplex.AddButtonDialogSingle;
import de.jg_cody.Teraplex.AddButtonsDialogDouble;
import de.jg_cody.Teraplex.Cricketer;
import de.jg_cody.Teraplex.R;
import de.jg_cody.Teraplex.ui.rooms.ListAdapter;
import de.jg_cody.Teraplex.ui.rooms.ListItem;
import de.jg_cody.Teraplex.ui.rooms.ListItemDouble;
import de.jg_cody.Teraplex.ui.rooms.ListItemSingle;

public class FlurFragment extends Fragment{

    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<>();

    @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) {

                }

                dialogInterface.dismiss();
            }
        });
        AlertDialog mDialog = mBuilder.create();
        Objects.requireNonNull(mDialog.getWindow()).setBackgroundDrawableResource(R.drawable.button_round);
        mDialog.show();
    }
    public void openDialog1() {
        AddButtonDialogSingle addButtonDialogSingle = new AddButtonDialogSingle();
        addButtonDialogSingle.show(requireActivity().getSupportFragmentManager(), "example dialog");
        items.add(new ListItemSingle(addButtonDialogSingle.getCommand(),1, addButtonDialogSingle.getName(), addButtonDialogSingle.getButtonName()));
        listAdapter.notifyDataSetChanged();


    }

    public void openDialog2() {
        AddButtonsDialogDouble addButtonsDialogDouble = new AddButtonsDialogDouble();
        addButtonsDialogDouble.show(requireActivity().getSupportFragmentManager(), "example dialog");
        items.add(new ListItemDouble(2,addButtonsDialogDouble.getName(), addButtonsDialogDouble.getCommand1(), addButtonsDialogDouble.getCommand2(), addButtonsDialogDouble.getButton1(), addButtonsDialogDouble.getButton2()));
        listAdapter.notifyDataSetChanged();
    }
    public void addItems(View v) {
        listAdapter.notifyDataSetChanged();
    }
}