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.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;
import com.ttiglobal.inttilectgpchat.R;

import java.util.Objects;

public class BlogDetails extends AppCompatActivity {

    private ImageView post_image, user_image,commentbtn;
    private TextView user_name, date, post_title, post_desc, like_count, txt_comment_coun, txt_post_time, txtName;
    private LinearLayout lloutComments, lloutLikes;
    private FirebaseFirestore firebaseFirestore;
    private FirebaseAuth mAuth;
    private Bundle bundle;

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

        post_image = findViewById(R.id.img_post_image);
        user_image = findViewById(R.id.img_profile_image);
        user_name = findViewById(R.id.txt_user_name);
//        date = findViewById(R.id.date);
        txt_post_time = findViewById(R.id.txt_post_time);
        post_title = findViewById(R.id.txt_post_title);
        post_desc = findViewById(R.id.txt_post_desc);
        commentbtn = findViewById(R.id.comment_bt);

        txt_comment_coun = findViewById(R.id.txt_comment_count);
        like_count = findViewById(R.id.txt_count);
        lloutComments = findViewById(R.id.llout_comments);
        lloutLikes = findViewById(R.id.llout_likes);
        txtName = findViewById(R.id.txtName);


        bundle = getIntent().getExtras();
        String ImageUrl = bundle.getString("file_url");
        String User = bundle.getString("user_id");
        String User_name = bundle.getString("sender_name");
        String Title = bundle.getString("blog_title");
        String Details = bundle.getString("blog_desc");
        String comment_count = bundle.getString("comment_count");
        String likes = bundle.getString("likes");
        String datetime = bundle.getString("date_time");

        assert comment_count != null;
        if (comment_count.equalsIgnoreCase("0"))
            txt_comment_coun.setText("No Comments");
        else if (comment_count.equalsIgnoreCase("1"))
            txt_comment_coun.setText(comment_count + " Comment");
        else
            txt_comment_coun.setText(comment_count + " Comments");
        assert likes != null;

        if (likes.equalsIgnoreCase("0"))
            like_count.setText("0 Like");
        else if (likes.equalsIgnoreCase("1"))
            like_count.setText(likes + " Like");
        else
            like_count.setText(likes + " Likes");

        firebaseFirestore = FirebaseFirestore.getInstance();
        mAuth = FirebaseAuth.getInstance();

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("");
//        txtName.setText("FORUM");
        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);

        commentbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(BlogDetails.this, CommentActivity.class);
                intent.putExtra("ref_id", bundle.getString("ref_id"));
                startActivity(intent);

            }
        });

        lloutComments.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(BlogDetails.this, CommentActivity.class);
                intent.putExtra("ref_id", bundle.getString("ref_id"));
                startActivity(intent);
            }
        });

        setPostImage(ImageUrl);
        setUser(User);
        setTime(datetime);
        user_name.setText(User_name);
        post_title.setText(Title);
        post_desc.setText(Details);
    }

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

    private void setTime(String time) {
//        TimeFormatter timeFormatter = new TimeFormatter();
        txt_post_time.setText(time);
    }

    private void setPostImage(String url) {
        Glide.with(this)
                .applyDefaultRequestOptions(new RequestOptions())
                .load(url)
                .into(post_image);
    }

    private void setUser(final String user_id) {
        FirebaseFirestore db = FirebaseFirestore.getInstance();
        db.collection("forum_users")
                .get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if (task.isSuccessful()) {
                            for (QueryDocumentSnapshot document : task.getResult()) {
                                if (user_id.equalsIgnoreCase(user_id)) {
                                    String url = document.getData().get("user_profile_url").toString();
                                    Glide.with(getApplicationContext())
                                            .applyDefaultRequestOptions(new RequestOptions()
                                                    .placeholder(R.drawable.user1)
                                                    .error(R.drawable.user1)
                                                    .diskCacheStrategy(DiskCacheStrategy.ALL))
                                            .load(url)
                                            .into(user_image);
                                }
                            }
                        }
                    }
                });
    }
}