package com.example.mynotepad.MenuFeatures.Pics.Gallery;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

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

import com.example.mynotepad.Dialogs.CustomDialog;
import com.example.mynotepad.Dialogs.onCustomDialogClickListener;
import com.example.mynotepad.MenuFeatures.AllNotes.DataBase.DateEntity;
import com.example.mynotepad.MenuFeatures.AllNotes.DataBase.Note;
import com.example.mynotepad.MenuFeatures.AllNotes.DataBase.PicsEntity;
import com.example.mynotepad.MenuFeatures.AllNotes.MyNoteRecyclerView.MyNote;
import com.example.mynotepad.MenuFeatures.AllNotes.MyNoteRecyclerView.OnAllNotesItemClickListener;
import com.example.mynotepad.MenuFeatures.CustomToolbarOption;
import com.example.mynotepad.MenuFeatures.Pics.PicsActivity;
import com.example.mynotepad.R;

import java.util.ArrayList;
import java.util.List;

import static com.example.mynotepad.MainActivity.noteDataBase;

public class NotedPicsListFragment extends Fragment {

    private List<MyGalleryPic> myGalleryPics;
    private List<PicsEntity> picsEntityList;
    private GalleryAdaptor adapter;
    private CustomToolbarOption customToolbarOption;
    private MyGalleryPic myChosenPic;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        ((PicsActivity) getActivity()).getSupportActionBar().setTitle("Pics List");
        return inflater.inflate(R.layout.fragment_noted_pics, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        RecyclerView recyclerView = view.findViewById(R.id.picsRecyclerView);
        recyclerView.setHasFixedSize(true);
        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getContext(), 2);
        recyclerView.setLayoutManager(layoutManager);
        //todo add customtoolbar

        setData();
        adapter = new GalleryAdaptor(myGalleryPics);
        recyclerView.setAdapter(adapter);
        addListeners();
    }

    private void setData() {
        picsEntityList = noteDataBase.picsDao().getAllPicsNotes();
        myGalleryPics = new ArrayList<>();
        for (PicsEntity picsEntity : picsEntityList) {
            MyGalleryPic myGalleryPic = new MyGalleryPic(picsEntity.getUrl(), picsEntity.getTxt());
            myGalleryPics.add(myGalleryPic);
        }
    }

    private void addListeners() {

        // add listener to recyclerView
        adapter.setOnItemClickListener(new OnGalleryItemClickListener() {
            @Override
            public void onItemClicked(MyGalleryPic myGalleryPic) {
                myChosenPic = myGalleryPic;
                //todo show big img or go to a new fragment
                //set noteFragment data
                final Bundle bundle = new Bundle();
                bundle.putString("url", myGalleryPic.getUrl());
                bundle.putString("txt", myGalleryPic.getText());
            }

            @Override
            public void onItemLongClicked(MyGalleryPic myGalleryPic) {
                myChosenPic = myGalleryPic;
            }
        });
    }


    protected void closeClicked(CustomToolbarOption customToolbarOption) {
        customToolbarOption.setVisibility(View.GONE);
    }

    protected void deleteClicked(final PicsEntity pic) {
        CustomDialog customDialog = new CustomDialog();
        customDialog.showAddTxtDialog(getActivity(), "are you sure you wanna delete it ??? ");
        customDialog.setOnCustomDialogClickListener(new onCustomDialogClickListener() {
            @Override
            public void yesClicked() {
                noteDataBase.picsDao().deletePicsNote(pic);
                myGalleryPics.remove(myChosenPic);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void noClicked() {
            }
        });
       // allNoteToolBar.setVisibility(View.VISIBLE);
        customToolbarOption.setVisibility(View.GONE);
       // allNoteToolBar.setVisibility(View.VISIBLE);
    }

}
