package com.lc.myapplication.security.mysoundstwo.fragment;

import android.content.Intent;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.lc.myapplication.security.mysoundstwo.R;
import com.lc.myapplication.security.mysoundstwo.ScaryPrank;
import com.lc.myapplication.security.mysoundstwo.activity.PreviewActivity;
import com.lc.myapplication.security.mysoundstwo.adapter.FavouriteAdapter;
import com.lc.myapplication.security.mysoundstwo.adapter.WinAdapter;
import com.lc.myapplication.security.mysoundstwo.databinding.FragmentWinBinding;
import com.lc.myapplication.security.mysoundstwo.list.SoundsData;
import com.lc.myapplication.security.mysoundstwo.room.ScaryPrankLikeDatabase;
import com.lc.myapplication.security.mysoundstwo.room.ScaryPrankLikeEntity;
import com.lc.myapplication.security.mysoundstwo.tool.MyTool;
import com.lc.myapplication.security.mysoundstwo.value.MyValue;

import java.util.List;

public class WinFragment extends Fragment {
    private TextView win_title;
    private int c = 0;
    private RecyclerView win_recycle;
    private ImageView win_one_image;
    private ImageView win_two_image;
    private ImageView win_three_image;
    private ImageView win_one_like;
    private ImageView win_two_like;
    private ImageView win_three_like;
    private boolean like1;
    private boolean like2;
    private boolean like3;
    private ScaryPrankLikeEntity list1;
    private ScaryPrankLikeEntity list2;
    private ScaryPrankLikeEntity list3;
    private List<ScaryPrankLikeEntity> entityList;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =  inflater.inflate(R.layout.fragment_win, container, false);
        win_title = view.findViewById(R.id.win_title);
        win_title.setTypeface(Typeface.createFromAsset(getActivity().getAssets(),"myfront1.ttf"));
        win_recycle = view.findViewById(R.id.win_recycle);
        win_one_image = view.findViewById(R.id.win_one_image);
        win_one_like = view.findViewById(R.id.win_one_like);
        win_two_image = view.findViewById(R.id.win_two_image);
        win_two_like = view.findViewById(R.id.win_two_like);
        win_three_image = view.findViewById(R.id.win_three_image);
        win_three_like = view.findViewById(R.id.win_three_like);
        gotogetimage();
        onMyclick();
        return view;
    }

    private void Getislike() {
       like1 = entityList.get(13).getSoundslike();
       if (like1){
           win_one_like.setBackgroundResource(R.drawable.win_like);
       }else {
           win_one_like.setBackgroundResource(R.drawable.win_unlike);
       }
       like2 = entityList.get(13).getSoundslike();
       if (like2){
           win_one_like.setBackgroundResource(R.drawable.win_like);
       }else {
           win_one_like.setBackgroundResource(R.drawable.win_unlike);
       }
       like3 = entityList.get(13).getSoundslike();
       if (like3){
           win_one_like.setBackgroundResource(R.drawable.win_like);
       }else {
           win_one_like.setBackgroundResource(R.drawable.win_unlike);
       }
    }


    private void gotogetimage() {
        MyTool.RunIO(new Runnable() {
            @Override
            public void run() {
                entityList = ScaryPrankLikeDatabase.getScaryPrankLikeDatabase().getScaryPrankLikeDao().getAllMessage();
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Glide.with(getActivity())
                                .load(entityList.get(5).getImage())
                                .transform(new RoundedCorners(90))
                                .listener(new RequestListener<Drawable>() {
                                    @Override
                                    public boolean onLoadFailed(@Nullable GlideException e, @Nullable Object model, @NonNull Target<Drawable> target, boolean isFirstResource) {
                                        //Log.e(MBrManager.TAG, "onLoadFailed-----------------categorylist.getCategoryUrl()=---"+categorylist.getCategoryUrl()+"---------e="+e.getMessage());
                                        return false;
                                    }

                                    @Override
                                    public boolean onResourceReady(@NonNull Drawable resource, @NonNull Object model, Target<Drawable> target, @NonNull DataSource dataSource, boolean isFirstResource) {
                                        return false;
                                    }
                                })
                                .into(win_one_image);
                        Glide.with(getActivity())
                                .load(entityList.get(115).getImage())
                                .transform(new RoundedCorners(90))
                                .listener(new RequestListener<Drawable>() {
                                    @Override
                                    public boolean onLoadFailed(@Nullable GlideException e, @Nullable Object model, @NonNull Target<Drawable> target, boolean isFirstResource) {
                                        //Log.e(MBrManager.TAG, "onLoadFailed-----------------categorylist.getCategoryUrl()=---"+categorylist.getCategoryUrl()+"---------e="+e.getMessage());
                                        return false;
                                    }

                                    @Override
                                    public boolean onResourceReady(@NonNull Drawable resource, @NonNull Object model, Target<Drawable> target, @NonNull DataSource dataSource, boolean isFirstResource) {
                                        return false;
                                    }
                                })
                                .into(win_two_image);
                        Glide.with(getActivity())
                                .load(entityList.get(125).getImage())
                                .transform(new RoundedCorners(90))
                                .listener(new RequestListener<Drawable>() {
                                    @Override
                                    public boolean onLoadFailed(@Nullable GlideException e, @Nullable Object model, @NonNull Target<Drawable> target, boolean isFirstResource) {
                                        //Log.e(MBrManager.TAG, "onLoadFailed-----------------categorylist.getCategoryUrl()=---"+categorylist.getCategoryUrl()+"---------e="+e.getMessage());
                                        return false;
                                    }

                                    @Override
                                    public boolean onResourceReady(@NonNull Drawable resource, @NonNull Object model, Target<Drawable> target, @NonNull DataSource dataSource, boolean isFirstResource) {
                                        return false;
                                    }
                                })
                                .into(win_three_image);
                        Getislike();
                        setRecycle();
                    }
                });
            }
        });
    }

    private void setRecycle() {
        WinAdapter winAdapter = new WinAdapter(getActivity(), entityList.subList(123,129));
        win_recycle.setAdapter(winAdapter);
        win_recycle.setLayoutManager(new GridLayoutManager(getContext(),1));
    }
    private void onMyclick() {
        win_one_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               gotoIntent(entityList.get(5));
            }
        });
        win_two_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoIntent(entityList.get(115));
            }
        });
        win_three_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoIntent(entityList.get(125));
            }
        });
        win_one_like.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (like1){
                    win_one_like.setBackgroundResource(R.drawable.win_unlike);
                    entityList.get(5).setSoundslike(false);
                    like1 = false;
                }else {
                    win_one_like.setBackgroundResource(R.drawable.win_like);
                    entityList.get(5).setSoundslike(true);
                    like1 = true;
                }
                MyTool.RunIO(new Runnable() {
                    @Override
                    public void run() {
                        ScaryPrankLikeDatabase.getScaryPrankLikeDatabase().getScaryPrankLikeDao().UpdateLikeSounds(entityList.get(5));
                    }
                });
            }
        });
        win_two_like.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (like2){
                    win_one_like.setBackgroundResource(R.drawable.win_unlike);
                    entityList.get(115).setSoundslike(false);
                    like1 = false;
                }else {
                    win_one_like.setBackgroundResource(R.drawable.win_like);
                    entityList.get(115).setSoundslike(true);
                    like1 = true;
                }
                MyTool.RunIO(new Runnable() {
                    @Override
                    public void run() {
                        ScaryPrankLikeDatabase.getScaryPrankLikeDatabase().getScaryPrankLikeDao().UpdateLikeSounds(entityList.get(115));
                    }
                });
            }
        });
        win_three_like.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (like3){
                    win_one_like.setBackgroundResource(R.drawable.win_unlike);
                    entityList.get(125).setSoundslike(false);
                    like1 = false;
                }else {
                    win_one_like.setBackgroundResource(R.drawable.win_like);
                    entityList.get(125).setSoundslike(true);
                    like1 = true;
                }
                MyTool.RunIO(new Runnable() {
                    @Override
                    public void run() {
                        ScaryPrankLikeDatabase.getScaryPrankLikeDatabase().getScaryPrankLikeDao().UpdateLikeSounds(entityList.get(125));
                    }
                });
            }
        });
    }
    private void gotoIntent(ScaryPrankLikeEntity data) {
        String putbg = String.valueOf(R.color.sounds_poupur_bg);
        Intent intent = new Intent(getContext(), PreviewActivity.class);
        intent.putExtra(MyValue.DATA_NAME,data.getName());
        intent.putExtra(MyValue.DATA_BG,putbg);
        getActivity().startActivity(intent);
    }

    @Override
    public void onResume() {
        super.onResume();
        gotogetimage();
    }
}