package com.example.onetap_ssh.ui.tabs;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

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

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;

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

import com.example.onetap_ssh.AddButtonsDialogDouble;
import com.example.onetap_ssh.MainActivity;
import com.example.onetap_ssh.R;

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

	private AddButtonsDialogDouble.AddButtonsDialogListenerDouble listener;
	private EditText editText_name, editText_command, editText_command1, editText_button1, editText_command2, editText_button2, editText_button_name;
	private TabsFragment frag;

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

	private final LayoutInflater inflater;
	private final String tabname;
	private final ArrayList<ListItem> objects;
	private final Context context;

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

	@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);
		notifyDataSetChanged();
	}

	public void remove(ListItem item) {
		int position = objects.indexOf(item);
		objects.remove(item);
		notifyItemRemoved(position);
	}

	public void remove(int position) {
		objects.remove(position);
		notifyItemRemoved(position);
	}

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

	public void clear() {
		objects.clear();
		notifyDataSetChanged();
	}

	public ArrayList<ListItem> getObjects() {
		return objects;
	}

	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();
		ImageView Remove = view.findViewById(R.id.image_remove);
		ImageView Edit = view.findViewById(R.id.image_edit);


		if (MainActivity.editmode) {
			Remove.setVisibility(View.VISIBLE);
			Edit.setVisibility(View.VISIBLE);
		}
		else  {
			Remove.setVisibility(View.GONE);
			Edit.setVisibility(View.GONE);
		}

		Remove.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				remove(item);
				try {
					TabsFragment.save(tabname, context, ListAdapter.this);
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		});
		Edit.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				switch (item.getType()) {
					case SINGLEBUTTON: {
						AlertDialog.Builder mBuilder = new AlertDialog.Builder(v.getContext());

						View view = inflater.inflate(R.layout.addbuttondialog_single, null);

						editText_command = view.findViewById(R.id.dialogSingle_editText_command);
						editText_command.setText(strings[1]);
						editText_name = view.findViewById(R.id.dialogheadline_editText_name);
						editText_name.setText(strings[0]);
						editText_button_name = view.findViewById(R.id.dialogSingle_editText_buttonName);
						editText_button_name.setText(strings[2]);


						mBuilder.setView(view)
								.setTitle("SINGLE BUTTON")
								.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
									@Override
									public void onClick(DialogInterface dialogInterface, int i) {

									}
								})
								.setPositiveButton(R.string.add, new DialogInterface.OnClickListener() {
									@Override
									public void onClick(DialogInterface dialogInterface, int i) {
										String command = editText_command.getText().toString();
										String name = editText_name.getText().toString();
										String button = editText_button_name.getText().toString();
										if(command.isEmpty()||button.isEmpty()||name.isEmpty()){

											AlertDialog mDialog = new AlertDialog.Builder(v.getContext())
													.setTitle(R.string.invalid)
													.setMessage(R.string.inputfields_cant_be_empty)

													// 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, null )
													.create();
											Objects.requireNonNull(mDialog.getWindow()).setBackgroundDrawableResource(R.drawable.button_round);
											mDialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation; //style id
											mDialog.show();

											return;
										}
										ListItemSingle item_tmp = (ListItemSingle) item;
										item_tmp.setButtonName(button);
										item_tmp.setCommand(command);
										item_tmp.setName(name);
										notifyDataSetChanged();
										try {
											TabsFragment.save(tabname, context, ListAdapter.this);
										} catch (JSONException e) {
											e.printStackTrace();
										}




									}
								});


						AlertDialog mDialog = mBuilder.create();
						Objects.requireNonNull(mDialog.getWindow()).setBackgroundDrawableResource(R.drawable.button_round);
						mDialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation; //style id
						mDialog.show();
						break;
					}
					case DOUBLEBUTTON: {

						AlertDialog.Builder mBuilder = new AlertDialog.Builder(v.getContext());


						View view = inflater.inflate(R.layout.addbuttonsdialog_double, null);

						mBuilder.setView(view)
								.setTitle("ON/OFF BUTTON")
								.setNegativeButton(R.string.cancel, null)
								.setPositiveButton(R.string.add, new DialogInterface.OnClickListener() {
									@Override
									public void onClick(DialogInterface dialogInterface, int i) {
										String command1 = editText_command1.getText().toString();
										String command2 = editText_command2.getText().toString();
										String name = editText_name.getText().toString();
										String button1 = editText_button1.getText().toString();
										String button2 = editText_button2.getText().toString();
										if (command1.isEmpty() || command2.isEmpty() || name.isEmpty() || button1.isEmpty() || button2.isEmpty()) {

											AlertDialog mDialog = new AlertDialog.Builder(v.getContext())
													.setTitle(R.string.invalid)
													.setMessage(R.string.inputfields_cant_be_empty)

													// 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, null)
													.create();
											Objects.requireNonNull(mDialog.getWindow()).setBackgroundDrawableResource(R.drawable.button_round);
											mDialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation; //style id
											mDialog.show();

											return;
										}
										ListItemDouble item_tmp = (ListItemDouble) item;
										item_tmp.setButton1(button1);
										item_tmp.setButton2(button2);
										item_tmp.setCommand1(command1);
										item_tmp.setCommand2(command2);
										item_tmp.setName(name);
										notifyDataSetChanged();
										try {
											TabsFragment.save(tabname, context, ListAdapter.this);
										} catch (JSONException e) {
											e.printStackTrace();
										}

									}
								});

						editText_name = view.findViewById(R.id.dialogDouble_editText_name);
						editText_name.setText(strings[0]);
						editText_command1 = view.findViewById(R.id.dialogDouble_editText_command1);
						editText_command1.setText(strings[1]);
						editText_command2 = view.findViewById(R.id.dialogDouble_editText_command2);
						editText_command2.setText(strings[2]);
						editText_button1 = view.findViewById(R.id.dialogDouble_editText_button1);
						editText_button1.setText(strings[3]);
						editText_button2 = view.findViewById(R.id.dialogDouble_editText_button2);
						editText_button2.setText(strings[4]);

						AlertDialog mDialog = mBuilder.create();
						Objects.requireNonNull(mDialog.getWindow()).setBackgroundDrawableResource(R.drawable.button_round);
						mDialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation; //style id
						mDialog.show();
						break;
					}
					case HEADLINE: {
						AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());

						View view = inflater.inflate(R.layout.addheadlinedialog, null);

						editText_name = view.findViewById(R.id.dialogheadline_editText_name);
						editText_name.setText(strings[0]);


						builder.setView(view)
								.setTitle(R.string.heading)
								.setNegativeButton(R.string.cancel, (dialogInterface, i) -> {

								})
								.setPositiveButton(R.string.add, (dialogInterface, i) -> {
									String headline = editText_name.getText().toString();
									if(headline.isEmpty()){

										AlertDialog mDialog = new AlertDialog.Builder(v.getContext())
												.setTitle(R.string.invalid)
												.setMessage(R.string.inputfields_cant_be_empty)

												// 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, null )
												.create();
										Objects.requireNonNull(mDialog.getWindow()).setBackgroundDrawableResource(R.drawable.button_round);
										mDialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation; //style id
										mDialog.show();
										return;
									}
									ListItemHeadline item_tmp = (ListItemHeadline) item;
									item_tmp.setName(headline);
									notifyDataSetChanged();
									try {
										TabsFragment.save(tabname, context, ListAdapter.this);
									} catch (JSONException e) {
										e.printStackTrace();
									}
								});


						AlertDialog mDialog = builder.create();
						Objects.requireNonNull(mDialog.getWindow()).setBackgroundDrawableResource(R.drawable.button_round);
						mDialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation; //style id
						mDialog.show();
						break;
					}
				}

			}});
		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]);
				bRun.setOnClickListener(new CommandExecutor(strings[1]));
				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]);
				bLeft.setOnClickListener(new CommandExecutor(strings[1]));
				bRight.setOnClickListener(new CommandExecutor(strings[2]));
				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;
		}
	}

}
