package com.fun.funnyprank.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.fun.funnyprank.R;
import com.fun.funnyprank.activity.PrewActivity;
import com.fun.funnyprank.adapter.RankAdapter;
import com.fun.funnyprank.data.LikeData;
import com.fun.funnyprank.data.MydataBase;
import com.fun.funnyprank.data.StaticValue;
import com.fun.funnyprank.databinding.FragmentRankBinding;
import com.fun.funnyprank.tools.Mytools;

import java.util.List;


public class RankFragment extends Fragment {
    FragmentRankBinding binding;
    private RankAdapter rankAdapter;
    private boolean islike1 = false;
    private boolean islike2 = false;
    private boolean islike3 = false;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentRankBinding.inflate(getLayoutInflater());

        rankAdapter = new RankAdapter(requireActivity());
        binding.rankRv.setLayoutManager(new LinearLayoutManager(requireContext()));
        initData();

        return binding.getRoot();
    }

    private void initData() {
        MydataBase.getInstance().likeDao().getRankdata(true).observe(getViewLifecycleOwner(), new Observer<List<LikeData>>() {
            @Override
            public void onChanged(List<LikeData> list) {
                LikeData mydata1 = list.get(0);
                LikeData mydata2 = list.get(1);
                LikeData mydata3 = list.get(2);

                Glide.with(requireContext())
                        .load(mydata1.getPreUrl())
                        .placeholder(R.mipmap.icon_logo)
                        .into(binding.rankImage1);
                if (mydata1.getTitle().length() > 6) {
                    String name = mydata1.getTitle().substring(0, 6);
                    binding.rankText1.setText(name + "...");
                } else {
                    binding.rankText1.setText(mydata1.getTitle());
                }

                binding.rankRelative1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(requireContext(), PrewActivity.class);
                        intent.putExtra(StaticValue.KEY_TITLE, mydata1.getTitle());
                        requireContext().startActivity(intent);
                    }
                });


                Glide.with(requireContext())
                        .load(mydata2.getPreUrl())
                        .placeholder(R.mipmap.icon_logo)
                        .into(binding.rankImage2);
                if (mydata2.getTitle().length() > 6) {
                    String name = mydata2.getTitle().substring(0, 6);
                    binding.rankText2.setText(name + "...");
                } else {
                    binding.rankText2.setText(mydata1.getTitle());
                }

                binding.rankRelative2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(requireContext(), PrewActivity.class);
                        intent.putExtra(StaticValue.KEY_TITLE, mydata2.getTitle());
                        requireContext().startActivity(intent);
                    }
                });


                Glide.with(requireContext())
                        .load(mydata3.getPreUrl())
                        .placeholder(R.mipmap.icon_logo)
                        .into(binding.rankImage3);
                if (mydata3.getTitle().length() > 6) {
                    String name = mydata3.getTitle().substring(0, 6);
                    binding.rankText3.setText(name + "...");
                } else {
                    binding.rankText3.setText(mydata1.getTitle());
                }

                binding.rankRelative3.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(requireContext(), PrewActivity.class);
                        intent.putExtra(StaticValue.KEY_TITLE, mydata3.getTitle());
                        requireContext().startActivity(intent);
                    }
                });


                List<LikeData> reList = list.subList(3, 9);
                rankAdapter.SetData(reList);
                binding.rankRv.setAdapter(rankAdapter);
            }
        });
    }
}