/*
 * Copyright (c) WhatsApp Inc. and its affiliates.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree.
 */

package stickeradmin.signalstickers.trending.stickermaker.WhatsAppBasedCode;

import android.content.Context;
import android.content.Intent;

import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import stickeradmin.signalstickers.trending.R;
import java.util.List;

public class StickerPackListAdapter extends RecyclerView.Adapter<StickerPackListItemViewHolder> {
    @NonNull
    private List<StickerPack> stickerPacks;
    @NonNull
    private final OnAddButtonClickedListener onAddButtonClickedListener;
    private int maxNumberOfStickersInARow;

    StickerPackListAdapter(@NonNull List<StickerPack> stickerPacks, @NonNull OnAddButtonClickedListener onAddButtonClickedListener) {
        this.stickerPacks = stickerPacks;
        this.onAddButtonClickedListener = onAddButtonClickedListener;
    }

    @NonNull
    @Override
    public StickerPackListItemViewHolder onCreateViewHolder(@NonNull final ViewGroup viewGroup, final int i) {
        final Context context = viewGroup.getContext();
        final LayoutInflater layoutInflater = LayoutInflater.from(context);
        final View stickerPackRow = layoutInflater.inflate(R.layout.loader_sticker_packs_list_item, viewGroup, false);
        return new StickerPackListItemViewHolder(stickerPackRow);
    }

    @Override
    public void onBindViewHolder(@NonNull final StickerPackListItemViewHolder viewHolder, final int index) {
        StickerPack pack = stickerPacks.get(index);

        viewHolder.addButton.setVisibility(View.GONE);
        //viewHolder.shareButton.setVisibility(View.GONE);

        if(pack.getCount()==0){
            //viewHolder.txt
//            viewHolder.imageRowView.setVisibility(View.GONE);
            viewHolder.addButton.setVisibility(View.GONE);
            viewHolder.addButton.setVisibility(View.GONE);
            viewHolder.txtmsg.setVisibility(View.VISIBLE);
            viewHolder.txtmsg.setText("Downloding...");

        }
        else{
            //viewHolder.imageRowView.setVisibility(View.VISIBLE);
            viewHolder.txtmsg.setVisibility(View.GONE);
            viewHolder.txtmsg.setText("Downloding...");
        }

        final Context context = viewHolder.publisherView.getContext();
        viewHolder.publisherView.setText("by " + pack.publisher);
        //viewHolder.filesizeView.setText(Formatter.formatShortFileSize(context, pack.getTotalSize()));

        viewHolder.titleView.setText(pack.name);

        if(pack.IsAnimated.equals("true") )
        {
            viewHolder.titleView.setCompoundDrawablesWithIntrinsicBounds(
                    R.drawable.play, 0, 0, 0);
        }

        viewHolder.container.setOnClickListener(view -> {
            Intent intent = new Intent(view.getContext(), StickerPackDetailsActivity.class);
            intent.putExtra(StickerPackDetailsActivity.EXTRA_SHOW_UP_BUTTON, true);
            intent.putExtra(StickerPackDetailsActivity.EXTRA_STICKER_PACK_DATA, pack.identifier);
            view.getContext().startActivity(intent);
        });
       // viewHolder.imageRowView.removeAllViews();
        //if this sticker pack contains less stickers than the max, then take the smaller size.
       // int actualNumberOfStickersToShow = Math.min(maxNumberOfStickersInARow, pack.getStickers().size());


        for (int i = 0; i < 4; i++) {
            try {
                Glide
                        .with(context)
                        .load(pack.getSticker(i).getUri())
                        .centerCrop()
                        .placeholder(R.drawable.sticker_error)
                        .into(viewHolder.girdItemImageview[i]);
            } catch (Exception ex) {
                // Toast.makeText(context, "error: array list size", Toast.LENGTH_LONG).show();
            }

        }

       // for (int i = 0; i < actualNumberOfStickersToShow; i++) {
          //  final SimpleDraweeView rowImage = (SimpleDraweeView) LayoutInflater.from(context).inflate(R.layout.loader_sticker_pack_list_item_image, viewHolder.imageRowView, false);
           // rowImage.setImageURI(pack.getSticker(i).getUri());

           // Glide
             //       .with(context)
             //       .load(pack.getSticker(i).getUri())
             //       .centerCrop()
             //      .placeholder(R.drawable.sticker_error)
             //       .into(rowImage);

            //final LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) rowImage.getLayoutParams();
          //  int marginBetweenImages =0;

         //   try {
                //marginBetweenImages =(viewHolder.imageRowView.getMeasuredWidth() - maxNumberOfStickersInARow * viewHolder.imageRowView.getContext().getResources().getDimensionPixelSize(R.dimen.sticker_pack_list_item_preview_image_size)) / (maxNumberOfStickersInARow - 1) - lp.leftMargin - lp.rightMargin;
         //   }
         //   catch(Exception ex)
         //   {}

          //  if (i != actualNumberOfStickersToShow - 1 && marginBetweenImages > 0) { //do not set the margin for the last image
          //      lp.setMargins(lp.leftMargin, lp.topMargin, lp.rightMargin + marginBetweenImages, lp.bottomMargin);
                //rowImage.setLayoutParams(lp);
          //  }
           // viewHolder.imageRowView.addView(rowImage);
       // }
        setAddButtonAppearance(viewHolder.addButton, pack);

        //viewHolder.shareButton.setOnClickListener(new View.OnClickListener() {
        //    @Override
        //    public void onClick(View view) {
        //        DataArchiver.createZipFileFromStickerPack(pack, context);
        //    }
        //});
    }

    private void setAddButtonAppearance(ImageView addButton, StickerPack pack) {
        if (pack.getIsWhitelisted()) {
            addButton.setImageResource(R.drawable.sticker_3rdparty_added);
            addButton.setClickable(false);
            addButton.setOnClickListener(null);

        } else {
            addButton.setImageResource(android.R.drawable.stat_sys_download);
            addButton.setOnClickListener(v -> onAddButtonClickedListener.onAddButtonClicked(pack));
            TypedValue outValue = new TypedValue();
            addButton.getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
            addButton.setBackgroundResource(outValue.resourceId);
        }
    }

    @Override
    public int getItemCount() {
        return stickerPacks.size();
    }

    void setMaxNumberOfStickersInARow(int maxNumberOfStickersInARow) {
        if (this.maxNumberOfStickersInARow != maxNumberOfStickersInARow) {
            this.maxNumberOfStickersInARow = maxNumberOfStickersInARow;
            notifyDataSetChanged();
        }
    }

    public interface OnAddButtonClickedListener {
        void onAddButtonClicked(StickerPack stickerPack);
    }
}
