package com.ttiglobal.inttilectgpchat.blog;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.firebase.ui.firestore.FirestoreRecyclerAdapter;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.annotations.NotNull;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;
import com.ttiglobal.inttilectgpchat.R;
import com.ttiglobal.inttilectgpchat.blog.Helper.HoViewHolder;
import com.ttiglobal.inttilectgpchat.blog.Modal.Blog;

import java.util.Objects;

public class BlogHoUser extends AppCompatActivity {

    TextView header;
    FirebaseAuth mAuth;
    private RecyclerView blog_list;
    private FirestoreRecyclerAdapter<Blog, HoViewHolder> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blog_ho_user);

        header = findViewById(R.id.header);
        mAuth = FirebaseAuth.getInstance();

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setContentInsetStartWithNavigation(0);
        toolbar.setTitle("");

        Objects.requireNonNull(getSupportActionBar()).setTitle("");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        blog_list = findViewById(R.id.blog_list);
        populateBlogList();
        blog_list.setAdapter(adapter);
        blog_list.setHasFixedSize(true);
        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        blog_list.setLayoutManager(linearLayoutManager);
        adapter.startListening();
    }

    private void populateBlogList() {
        Query query = FirebaseFirestore.getInstance()
                .collection("forum_datas")
                .orderBy("forum_title", Query.Direction.DESCENDING);

        FirestoreRecyclerOptions<Blog> options = new FirestoreRecyclerOptions.Builder<Blog>()
                .setQuery(query, Blog.class)
                .build();

        adapter = new FirestoreRecyclerAdapter<Blog, HoViewHolder>(options) {

            @Override
            public void onBindViewHolder(@NotNull HoViewHolder holder, int position, @NotNull final Blog model) {

                holder.setImage(model.getFile_url(), getApplicationContext());
                holder.setUser(model.getSender_name(), model.getThumb_url(), getApplicationContext());
                holder.setTitle(model.getBlog_title());
                holder.setDesc(model.getBlog_desc());
                holder.setApprove(model.getIs_ho_approved(), model.getRef_id());

//                    holder.showPostDetails(model.getSender_name(),model.getRef_id(), model.getFile_url(), model.getUser_id(), model.getFile_type(), model.getDate_time(), model.getBlog_title(), model.getBlog_desc(),model.getMessage().size(), com.ttiglobal.inttilectv2.blog.BlogHoUser.this);
            }

            @NotNull
            @Override
            public HoViewHolder onCreateViewHolder(@NotNull ViewGroup group, int i) {
                View view = LayoutInflater.from(group.getContext())
                        .inflate(R.layout.houserblog, group, false);
                return new HoViewHolder(view);
            }
        };
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }
}
