package de.jg_cody.Teraplex.ui.rooms;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Collections;

import de.jg_cody.Teraplex.R;

public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ListItemViewHolder> {

	public static final int
			SINGLEBUTTON = 0,
			DOUBLEBUTTON = 1,
			HEADLINE = 2,
			SPACE = 3;

	private Context context;
	private LayoutInflater inflater;
	private ArrayList<ListItem> objects;

	public ListAdapter(Context context, ArrayList<ListItem> objects) {
		//super(context, resource, objects);
		this.context = context;
		this.inflater = LayoutInflater.from(context);
		this.objects = objects;
	}

	@NonNull
	@NotNull
	@Override
	public ListItemViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
		return new ListItemViewHolder(createView(viewType, parent));
	}

	@Override
	public void onBindViewHolder(@NonNull @NotNull ListItemViewHolder holder, int position) {
		bindView(holder.getItemView(), objects.get(position));
	}

	public int getItemViewType(int position) {
		return objects.get(position).getType();
	}

	@Override
	public int getItemCount() {
		return objects.size();
	}

	public void add(ListItem item) {
		objects.add(item);
	}

	public void swap(int idx1, int idx2) {
		Collections.swap(objects, idx1, idx2);
		notifyItemMoved(idx1, idx2);
	}

	public View createView(int listViewItemType, ViewGroup parent) {
		switch(listViewItemType) {
			case SINGLEBUTTON:
			{
				return inflater.inflate(R.layout.singlebutton, parent, false);
			}
			case DOUBLEBUTTON:
			{
				return inflater.inflate(R.layout.doublebutton, parent, false);
			}
			case SPACE:
			{
				return inflater.inflate(R.layout.space, parent, false);
			}
			case HEADLINE:
			{
				return inflater.inflate(R.layout.headline, parent, false);
			}
		}

		return null;
	}

	public void bindView(View view, ListItem item) {
		String[] strings = item.getStrings();
		switch(item.getType()) {
			case SINGLEBUTTON:
			{
				Button bRun = view.findViewById(R.id.singleButton_button);
				Button text = view.findViewById(R.id.singleButton_text);

				bRun.setText(strings[2]);
				text.setText(strings[0]);
				break;
			}
			case DOUBLEBUTTON:
			{
				Button bLeft = (Button) view.findViewById(R.id.doubleButton_bLeft);
				Button bRight = (Button) view.findViewById(R.id.doubleButton_bRight);
				Button text = (Button) view.findViewById(R.id.doubleButton_text);

				bLeft.setText(strings[3]);
				bRight.setText(strings[4]);
				text.setText(strings[0]);
				break;
			}
			case SPACE:
			{
				// NOP
				break;
			}
			case HEADLINE:
			{
				Button text = (Button) view.findViewById(R.id.headline_text);
				text.setText(strings[0]);
				break;
			}
		}
	}

	public static class ListItemViewHolder extends RecyclerView.ViewHolder {

		private View itemView;

		public ListItemViewHolder(@NonNull @NotNull View itemView) {
			super(itemView);
			this.itemView = itemView;
		}

		public void setItemView(View itemView) {
			this.itemView = itemView;
		}

		public View getItemView() {
			return itemView;
		}
	}
}
