package app.fedilab.fedilabtube.drawer;
/* Copyright 2020 Thomas Schneider
 *
 * This file is a part of TubeLab
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 *
 * TubeLab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with TubeLab; if not,
 * see <http://www.gnu.org/licenses>. */

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.ColorUtils;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import app.fedilab.fedilabtube.R;
import app.fedilab.fedilabtube.client.data.AccountData.Account;
import app.fedilab.fedilabtube.helper.Helper;


public class AccountsHorizontalListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    EventListener listener;
    private List<Account> accounts;
    private Context context;

    public AccountsHorizontalListAdapter(List<Account> accounts, EventListener listener) {
        this.accounts = accounts;
        this.listener = listener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        return new ViewHolder(layoutInflater.inflate(R.layout.drawer_horizontal_account, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
        final AccountsHorizontalListAdapter.ViewHolder holder = (AccountsHorizontalListAdapter.ViewHolder) viewHolder;
        final Account account = accounts.get(position);

        if (account.getDisplayName() != null && !account.getDisplayName().trim().equals(""))
            holder.account_dn.setText(account.getDisplayName());
        else
            holder.account_dn.setText(account.getUsername().replace("@", ""));

        //Profile picture
        Helper.loadGiF(context, account.getAvatar() != null ? account.getAvatar().getPath() : null, holder.account_pp, 270);

        if (account.isSelected()) {
            holder.main_container.setBackgroundColor(ColorUtils.setAlphaComponent(ContextCompat.getColor(context, Helper.getColorAccent()), 50));
        } else {
            holder.main_container.setBackgroundColor(Color.TRANSPARENT);
        }

    }

    @Override
    public long getItemId(int position) {
        return position;
    }

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


    public interface EventListener {
        void click(String forAccount);
    }


    private class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        ImageView account_pp;
        TextView account_dn;
        LinearLayout main_container;

        ViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
            account_pp = itemView.findViewById(R.id.account_pp);
            account_dn = itemView.findViewById(R.id.account_dn);
            main_container = itemView.findViewById(R.id.main_container);
        }

        @Override
        public void onClick(View v) {
            Account account = accounts.get(getAdapterPosition());
            listener.click(account.getId());
            for (Account acc : accounts) {
                if (acc.getId().compareTo(account.getId()) == 0) {
                    acc.setSelected(true);
                } else {
                    acc.setSelected(false);
                }
            }
            notifyItemRangeChanged(0, accounts.size());
        }
    }

}