package com.ttiglobal.inttilectgpchat.blog;

import android.content.Intent;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
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.android.material.floatingactionbutton.FloatingActionButton;
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.BlogViewHolder;
import com.ttiglobal.inttilectgpchat.blog.Modal.Blog;


import java.util.Objects;

public class BlogActivity extends AppCompatActivity {

    TextView header;
    FirebaseAuth mAuth;
    private FirestoreRecyclerAdapter<Blog, BlogViewHolder> adapter;

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

        FloatingActionButton fabBtn = findViewById(R.id.fabBtn);
        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);

        Drawable backArrow = getResources().getDrawable(R.drawable.abc_ic_ab_back_material);
        backArrow.setColorFilter(getResources().getColor(R.color.txt_color), PorterDuff.Mode.SRC_ATOP);
        getSupportActionBar().setHomeAsUpIndicator(backArrow);

        RecyclerView 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();

        fabBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(BlogActivity.this, PostActivity.class));
            }
        });
    }

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

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

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

            @Override
            public void onBindViewHolder(@NotNull BlogViewHolder holder, int position, @NotNull final Blog model) {
//                if (model.getIs_ho_approved() != null && model.getIs_ho_approved().equalsIgnoreCase("1")) {
                    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.setCommentCount(model.getMessage().size());
                    holder.setLikes(BlogActivity.this, model.getRef_id(), model.getUser_id());
                    holder.showPostDetails(model.getIs_ho_approved(), model.getIs_ho_rejected(), 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(), BlogActivity.this);
//                } else {
//                    holder.setVisibilty();
//                }
            }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            super.onBackPressed();
        }
        return true;
    }

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