package com.example.mynotepad.Dialogs.FoldersCustomDialog;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mynotepad.MainActivity;
import com.example.mynotepad.R;

public class FoldersDialog {
    private RecyclerView recyclerView;
    private LinearLayout addNewFolder;
    private String selectedFolderName;
    private FolderRecyclerViewAdapter folderRecyclerViewAdapter;
    public void showDialog(final Activity activity){
        final Dialog dialog = new Dialog(activity);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        dialog.setCancelable(true);
        dialog.setContentView(R.layout.dialog_folders);
        addNewFolder = dialog.findViewById(R.id.addFolder);
        recyclerView = dialog.findViewById(R.id.recyclerView2);
        if (MainActivity.sharedPreferencesClass.getArrayFolders(activity,"folders")!= null) {
            folderRecyclerViewAdapter = new FolderRecyclerViewAdapter(MainActivity.sharedPreferencesClass.getArrayFolders(activity, "folders"), activity);
            recyclerView.setAdapter(folderRecyclerViewAdapter);
            recyclerView.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false));
            folderRecyclerViewAdapter.setOnFolderClickListener(new OnFolderClickListener() {
                @Override
                public void itemClicked(String folderName) {
                    selectedFolderName = folderName;
                    dialog.dismiss();
                }
            });
        }
        dialog.show();

        addNewFolder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CreateNewFolderDialog createNewFolderDialog = new CreateNewFolderDialog();
                createNewFolderDialog.showDialog(activity);
                folderRecyclerViewAdapter.notifyDataSetChanged();
            }
        });
    }
}
