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.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
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.recyclerview.widget.RecyclerView;

import java.util.List;

import app.fedilab.fedilabtube.PeertubeActivity;
import app.fedilab.fedilabtube.PeertubeEditUploadActivity;
import app.fedilab.fedilabtube.R;
import app.fedilab.fedilabtube.ShowChannelActivity;
import app.fedilab.fedilabtube.client.data.VideoData;
import app.fedilab.fedilabtube.helper.Helper;


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

    private List<VideoData.Video> videos;
    private Context context;
    private boolean myVideos;

    public PeertubeAdapter(List<VideoData.Video> videos, boolean myVideos) {
        this.videos = videos;
        this.myVideos = myVideos;
    }

    public PeertubeAdapter(List<VideoData.Video> videos) {
        this.videos = videos;
        this.myVideos = false;
    }

    @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_peertube, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {


        final PeertubeAdapter.ViewHolder holder = (PeertubeAdapter.ViewHolder) viewHolder;
        final VideoData.Video video = videos.get(position);


        SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
        String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, "");

        assert userId != null;


        boolean ownVideos;
        if (myVideos) {
            ownVideos = true;
        } else {
            ownVideos = video.getAccount() != null && video.getAccount().getId() != null && video.getAccount().getHost().compareTo(Helper.getLiveInstance(context)) == 0 && video.getAccount().getId().compareTo(userId) == 0;
        }

        holder.peertube_account_name.setText(video.getChannel().getAcct());
        Helper.loadGiF(context, video.getChannel().getAvatar() != null ? video.getChannel().getAvatar().getPath() : null, holder.peertube_profile);

        holder.peertube_title.setText(video.getName());
        holder.peertube_duration.setText(Helper.secondsToString(video.getDuration()));
        holder.peertube_date.setText(String.format(" - %s", Helper.dateDiff(context, video.getCreatedAt())));
        holder.peertube_views.setText(context.getString(R.string.number_view_video, Helper.withSuffix(video.getViews())));

        Helper.loadGiF(context, video.getThumbnailPath(), holder.peertube_video_image);

        //For Overview Videos: boolean values for displaying title is managed in the fragment
        if (video.isHasTitle()) {
            holder.header_title.setVisibility(View.VISIBLE);
            switch (video.getTitleType()) {
                case TAG:
                    holder.header_title.setText(String.format("#%s", video.getTitle()));
                    break;
                case CHANNEL:
                case CATEGORY:
                    holder.header_title.setText(String.format("%s", video.getTitle()));
                    break;
            }
        } else {
            holder.header_title.setVisibility(View.GONE);
        }

        if (!ownVideos) {
            holder.peertube_profile.setOnClickListener(v -> {
                Intent intent = new Intent(context, ShowChannelActivity.class);
                Bundle b = new Bundle();
                b.putParcelable("channel", video.getChannel());
                intent.putExtras(b);
                context.startActivity(intent);
            });
        }


        if (!ownVideos) {
            holder.bottom_container.setOnClickListener(v -> {
                Intent intent = new Intent(context, PeertubeActivity.class);
                Bundle b = new Bundle();
                b.putString("video_id", video.getId());
                intent.putExtras(b);
                context.startActivity(intent);
            });
        } else {
            holder.bottom_container.setOnClickListener(v -> {
                Intent intent = new Intent(context, PeertubeEditUploadActivity.class);
                Bundle b = new Bundle();
                b.putString("video_id", video.getUuid());
                intent.putExtras(b);
                context.startActivity(intent);
            });
        }
        holder.peertube_video_image.setOnClickListener(v -> {
            Intent intent = new Intent(context, PeertubeActivity.class);
            Bundle b = new Bundle();
            b.putString("video_id", video.getId());
            intent.putExtras(b);
            context.startActivity(intent);
        });

    }

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

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


    static class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout main_container, bottom_container;
        ImageView peertube_profile, peertube_video_image;
        TextView peertube_account_name, peertube_views, peertube_duration;
        TextView peertube_title, peertube_date, header_title;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            peertube_account_name = itemView.findViewById(R.id.peertube_account_name);
            peertube_title = itemView.findViewById(R.id.peertube_title);
            peertube_video_image = itemView.findViewById(R.id.peertube_video_image);
            peertube_profile = itemView.findViewById(R.id.peertube_profile);
            peertube_date = itemView.findViewById(R.id.peertube_date);
            peertube_views = itemView.findViewById(R.id.peertube_views);
            peertube_duration = itemView.findViewById(R.id.peertube_duration);
            main_container = itemView.findViewById(R.id.main_container);
            header_title = itemView.findViewById(R.id.header_title);
            bottom_container = itemView.findViewById(R.id.bottom_container);
        }
    }


}