package com.fun.airhornprank.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 android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.fun.airhornprank.PrankSkin;
import com.fun.airhornprank.R;
import com.fun.airhornprank.activity.PreViewActivity;
import com.fun.airhornprank.adapter.RankAdapter;
import com.fun.airhornprank.data.MyDataBase;
import com.fun.airhornprank.data.Mydata;
import com.fun.airhornprank.databinding.FragmentRankingBinding;
import com.fun.airhornprank.tool.Mytools;
import com.fun.airhornprank.tool.StaticValue;

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

public class RankingFragment extends Fragment {
    FragmentRankingBinding 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 = FragmentRankingBinding.inflate(getLayoutInflater());
        rankAdapter = new RankAdapter(requireActivity());
        binding.rankRv.setLayoutManager(new LinearLayoutManager(requireContext()));
        initRankData();
        return binding.getRoot();
    }

    private void initRankData() {
        MyDataBase.getInstance().likeDAO().getRankdata(true).observe(getViewLifecycleOwner(), new Observer<List<Mydata>>() {
            @Override
            public void onChanged(List<Mydata> list) {
                Mydata mydata1 = list.get(0);
                Mydata mydata2 = list.get(1);
                Mydata mydata3 = list.get(2);
                Glide.with(requireContext())
                        .load(mydata1.getPreUrl())
                        .placeholder(R.mipmap.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());
                }

                Mytools.runIO(new Runnable() {
                    @Override
                    public void run() {
                        List<Mydata> list = MyDataBase.getInstance().likeDAO().getliketitle(mydata1.getTitle());
                        Mydata mydata = list.get(0);
                        requireActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (mydata.getLike()) {
                                    binding.rankLike1.setBackgroundResource(R.drawable.icon_like);
                                } else {
                                    binding.rankLike1.setBackgroundResource(R.drawable.icon_unlike);
                                }
                            }
                        });
                    }
                });
                binding.rankRelative1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(requireContext(), PreViewActivity.class);
                        intent.putExtra(StaticValue.KEY_TITLE, mydata1.getTitle());
                        requireContext().startActivity(intent);
                    }
                });
                binding.rankLike1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (!islike1) {
                            islike1 = true;
                            mydata1.setLike(true);
                            binding.rankLike1.setBackgroundResource(R.drawable.icon_like);
                            Mytools.runIO(new Runnable() {
                                @Override
                                public void run() {
                                    MyDataBase.getInstance().likeDAO().update(mydata1);
                                }
                            });
                        } else {
                            islike1 = false;
                            mydata1.setLike(false);
                            binding.rankLike1.setBackgroundResource(R.drawable.icon_unlike);
                            Mytools.runIO(new Runnable() {
                                @Override
                                public void run() {
                                    MyDataBase.getInstance().likeDAO().update(mydata1);
                                }
                            });
                        }
                    }
                });

                Glide.with(requireContext())
                        .load(mydata2.getPreUrl())
                        .placeholder(R.mipmap.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());
                }
                Mytools.runIO(new Runnable() {
                    @Override
                    public void run() {
                        List<Mydata> list = MyDataBase.getInstance().likeDAO().getliketitle(mydata2.getTitle());
                        Mydata mydata = list.get(0);
                        requireActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (mydata.getLike()) {
                                    binding.rankLike2.setBackgroundResource(R.drawable.icon_like);
                                } else {
                                    binding.rankLike2.setBackgroundResource(R.drawable.icon_unlike);
                                }
                            }
                        });
                    }
                });
                binding.rankRelative2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(requireContext(), PreViewActivity.class);
                        intent.putExtra(StaticValue.KEY_TITLE, mydata2.getTitle());
                        requireContext().startActivity(intent);
                    }
                });
                binding.rankLike2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (!islike2) {
                            islike2 = true;
                            mydata2.setLike(true);
                            binding.rankLike2.setBackgroundResource(R.drawable.icon_like);
                            Mytools.runIO(new Runnable() {
                                @Override
                                public void run() {
                                    MyDataBase.getInstance().likeDAO().update(mydata2);
                                }
                            });
                        } else {
                            islike2 = false;
                            mydata2.setLike(false);
                            binding.rankLike2.setBackgroundResource(R.drawable.icon_unlike);
                            Mytools.runIO(new Runnable() {
                                @Override
                                public void run() {
                                    MyDataBase.getInstance().likeDAO().update(mydata2);
                                }
                            });
                        }
                    }
                });

                Glide.with(requireContext())
                        .load(mydata3.getPreUrl())
                        .placeholder(R.mipmap.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());
                }
                Mytools.runIO(new Runnable() {
                    @Override
                    public void run() {
                        List<Mydata> list = MyDataBase.getInstance().likeDAO().getliketitle(mydata3.getTitle());
                        Mydata mydata = list.get(0);
                        requireActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (mydata.getLike()) {
                                    binding.rankLike3.setBackgroundResource(R.drawable.icon_like);
                                } else {
                                    binding.rankLike3.setBackgroundResource(R.drawable.icon_unlike);
                                }
                            }
                        });
                    }
                });
                binding.rankRelative3.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(requireContext(), PreViewActivity.class);
                        intent.putExtra(StaticValue.KEY_TITLE, mydata3.getTitle());
                        requireContext().startActivity(intent);
                    }
                });
                binding.rankLike3.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (!islike3) {
                            islike3 = true;
                            mydata3.setLike(true);
                            binding.rankLike3.setBackgroundResource(R.drawable.icon_like);
                            Mytools.runIO(new Runnable() {
                                @Override
                                public void run() {
                                    MyDataBase.getInstance().likeDAO().update(mydata3);
                                }
                            });
                        } else {
                            islike3 = false;
                            mydata3.setLike(false);
                            binding.rankLike3.setBackgroundResource(R.drawable.icon_unlike);
                            Mytools.runIO(new Runnable() {
                                @Override
                                public void run() {
                                    MyDataBase.getInstance().likeDAO().update(mydata3);
                                }
                            });
                        }
                    }
                });


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