package com.example.mynotepad.MenuFeatures.Folders;

import android.app.AlertDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.example.mynotepad.Dialogs.FoldersCustomDialog.FolderRecyclerViewAdapter;
import com.example.mynotepad.Dialogs.FoldersCustomDialog.OnFolderClickListener;
import com.example.mynotepad.MainActivity;
import com.example.mynotepad.MenuFeatures.AllNotes.AllNotesListFragment;
import com.example.mynotepad.MenuFeatures.CustomToolbarOption;
import com.example.mynotepad.MenuFeatures.Utils;
import com.example.mynotepad.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;


public class FoldersListFragment extends Fragment {

    private FolderRecyclerViewAdapter folderRecyclerViewAdapter;
    private RecyclerView recyclerView;
    private CustomToolbarOption customToolbarOption;
    private FrameLayout allNoteToolBar;
    private View listLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_foldersl_list, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init(view);
        setBg(Utils.sTheme);

    }

    private void init(View view) {
        listLayout = view.findViewById(R.id.allNoteListLayout);
        recyclerView = view.findViewById(R.id.recyclerView);
        customToolbarOption = view.findViewById(R.id.customToolbarOption);
        customToolbarOption.setVisibility(View.GONE);
        allNoteToolBar = view.findViewById(R.id.toolbar);
        allNoteToolBar.setEnabled(false);
      //  if (MainActivity.sharedPreferencesClass.getArrayFolders(getActivity(),"folders")!= null) {
        SharedPreferences  sharedPreferences = getActivity().getPreferences(Context.MODE_PRIVATE);
        Gson gson = new Gson();
        String json = sharedPreferences.getString("folders", null);
        System.out.println(json+"+++++++++++++++++++++++++++++++++++++++++++++++");

        Type type = new TypeToken<ArrayList<String>>() {}.getType();
        ArrayList<String> a = gson.fromJson(json, type);
            folderRecyclerViewAdapter = new FolderRecyclerViewAdapter(a, getActivity());
            recyclerView.setAdapter(folderRecyclerViewAdapter);
            recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
            folderRecyclerViewAdapter.setOnFolderClickListener(new OnFolderClickListener() {
                @Override
                public void itemClicked(String folderName) {

                }
            });
       // }
    }

    private void setBg(String sTheme) {
        switch (sTheme) {
            case "dark":
                listLayout.setBackgroundResource(R.drawable.main_bg_dark);
                break;
            case "bright":
                listLayout.setBackgroundResource(R.drawable.main_bg_bright);
                break;
        }
    }
}