package app.fedilab.fedilabtube.fragment;
/* 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.SharedPreferences;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import org.jetbrains.annotations.NotNull;

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

import app.fedilab.fedilabtube.R;
import app.fedilab.fedilabtube.client.APIResponse;
import app.fedilab.fedilabtube.client.RetrofitPeertubeAPI;
import app.fedilab.fedilabtube.client.data.AccountData.Account;
import app.fedilab.fedilabtube.client.data.VideoData;
import app.fedilab.fedilabtube.drawer.AccountsHorizontalListAdapter;
import app.fedilab.fedilabtube.drawer.PeertubeAdapter;
import app.fedilab.fedilabtube.helper.Helper;
import app.fedilab.fedilabtube.viewmodel.AccountsVM;
import app.fedilab.fedilabtube.viewmodel.SearchVM;
import app.fedilab.fedilabtube.viewmodel.TimelineVM;
import es.dmoral.toasty.Toasty;


public class DisplayVideosFragment extends Fragment implements AccountsHorizontalListAdapter.EventListener {


    private LinearLayoutManager mLayoutManager;
    private GridLayoutManager gLayoutManager;
    private boolean flag_loading;
    private Context context;
    private PeertubeAdapter peertubeAdapater;
    private AccountsHorizontalListAdapter accountsHorizontalListAdapter;
    private String max_id, max_id_accounts;
    private List<VideoData.Video> peertubes;
    private List<Account> accounts;
    private TimelineVM.TimelineType type;
    private RelativeLayout mainLoader, nextElementLoader, textviewNoAction;
    private boolean firstLoad;
    private SwipeRefreshLayout swipeRefreshLayout;
    private SharedPreferences sharedpreferences;
    private String search_peertube;
    private TextView textviewNoActionText;
    private View rootView;
    private RecyclerView lv_status;
    private boolean check_ScrollingUp;
    private String forAccount;
    private ConstraintLayout top_account_container;
    private TimelineVM viewModelFeeds;
    private SearchVM viewModelSearch;
    private AccountsVM viewModelAccounts;
    private String channelId;

    public DisplayVideosFragment() {
    }


    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_video, container, false);


        peertubes = new ArrayList<>();
        accounts = new ArrayList<>();
        context = getContext();
        Bundle bundle = this.getArguments();
        if (bundle != null) {
            search_peertube = bundle.getString("search_peertube", null);
            channelId = bundle.getString("channelId", null);
            type = (TimelineVM.TimelineType) bundle.get("timelineType");
        }
        max_id = "0";

        if (getArguments() != null && type == null) {
            type = DisplayVideosFragmentArgs.fromBundle(getArguments()).getType();
        }

        forAccount = null;
        lv_status = rootView.findViewById(R.id.lv_status);
        RecyclerView lv_accounts = rootView.findViewById(R.id.lv_accounts);
        Button display_all = rootView.findViewById(R.id.display_all);
        top_account_container = rootView.findViewById(R.id.top_account_container);
        max_id = null;
        max_id_accounts = null;
        flag_loading = true;
        firstLoad = true;
        check_ScrollingUp = false;

        assert context != null;
        sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
        swipeRefreshLayout = rootView.findViewById(R.id.swipeContainer);

        mainLoader = rootView.findViewById(R.id.loader);
        nextElementLoader = rootView.findViewById(R.id.loading_next_status);
        textviewNoAction = rootView.findViewById(R.id.no_action);
        textviewNoActionText = rootView.findViewById(R.id.no_action_text);
        mainLoader.setVisibility(View.VISIBLE);
        nextElementLoader.setVisibility(View.GONE);

        peertubeAdapater = new PeertubeAdapter(this.peertubes, type == TimelineVM.TimelineType.MY_VIDEOS);
        lv_status.setAdapter(peertubeAdapater);

        accountsHorizontalListAdapter = new AccountsHorizontalListAdapter(this.accounts, this);
        LinearLayoutManager layoutManager
                = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
        lv_accounts.setLayoutManager(layoutManager);
        lv_accounts.setAdapter(accountsHorizontalListAdapter);
        if (!Helper.isTablet(context)) {
            mLayoutManager = new LinearLayoutManager(context);
            lv_status.setLayoutManager(mLayoutManager);
        } else {
            gLayoutManager = new GridLayoutManager(context, 2);
            int spanCount = (int) Helper.convertDpToPixel(2, context);
            int spacing = (int) Helper.convertDpToPixel(5, context);
            lv_status.addItemDecoration(new GridSpacingItemDecoration(spanCount, spacing, true));
            lv_status.setLayoutManager(gLayoutManager);
        }
        viewModelAccounts = new ViewModelProvider(DisplayVideosFragment.this).get(AccountsVM.class);
        viewModelFeeds = new ViewModelProvider(DisplayVideosFragment.this).get(TimelineVM.class);
        viewModelSearch = new ViewModelProvider(DisplayVideosFragment.this).get(SearchVM.class);
        swipeRefreshLayout.setOnRefreshListener(this::pullToRefresh);

        lv_accounts.addOnScrollListener(new RecyclerView.OnScrollListener() {
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                int firstVisibleItem = layoutManager.findFirstVisibleItemPosition();
                if (dy > 0) {
                    int visibleItemCount = layoutManager.getChildCount();
                    int totalItemCount = layoutManager.getItemCount();
                    if (firstVisibleItem + visibleItemCount == totalItemCount && context != null) {
                        viewModelAccounts.getAccounts(RetrofitPeertubeAPI.DataType.SUBSCRIBER, max_id_accounts).observe(DisplayVideosFragment.this.requireActivity(), apiResponse -> manageViewAccounts(apiResponse));
                    }
                }
            }
        });


        lv_status.addOnScrollListener(new RecyclerView.OnScrollListener() {
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                if (type == TimelineVM.TimelineType.SUBSCRIBTIONS) {
                    if (dy > 0) {
                        if (check_ScrollingUp) {
                            top_account_container.setVisibility(View.GONE);
                            final Handler handler = new Handler();
                            handler.postDelayed(() -> check_ScrollingUp = false, 300);

                        }
                    } else {
                        if (!check_ScrollingUp) {
                            top_account_container.setVisibility(View.VISIBLE);
                            final Handler handler = new Handler();
                            handler.postDelayed(() -> check_ScrollingUp = true, 300);
                        }
                    }
                }
                if (mLayoutManager != null) {
                    int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
                    if (dy > 0) {
                        int visibleItemCount = mLayoutManager.getChildCount();
                        int totalItemCount = mLayoutManager.getItemCount();
                        if (firstVisibleItem + visibleItemCount == totalItemCount && context != null) {
                            if (!flag_loading) {
                                flag_loading = true;
                                loadTimeline(max_id);
                                nextElementLoader.setVisibility(View.VISIBLE);
                            }
                        } else {
                            nextElementLoader.setVisibility(View.GONE);
                        }
                    }
                } else if (gLayoutManager != null) {
                    int firstVisibleItem = gLayoutManager.findFirstVisibleItemPosition();
                    if (dy > 0) {
                        int visibleItemCount = gLayoutManager.getChildCount();
                        int totalItemCount = gLayoutManager.getItemCount();
                        if (firstVisibleItem + visibleItemCount == totalItemCount && context != null) {
                            if (!flag_loading) {
                                flag_loading = true;
                                loadTimeline(max_id);
                                nextElementLoader.setVisibility(View.VISIBLE);
                            }
                        } else {
                            nextElementLoader.setVisibility(View.GONE);
                        }
                    }
                }
            }
        });
        if (type == TimelineVM.TimelineType.SUBSCRIBTIONS) {
            AccountsVM viewModel = new ViewModelProvider(this).get(AccountsVM.class);
            viewModel.getAccounts(RetrofitPeertubeAPI.DataType.SUBSCRIBER, max_id).observe(DisplayVideosFragment.this.requireActivity(), this::manageViewAccounts);
        }
        loadTimeline(max_id);
        display_all.setOnClickListener(v -> {
            forAccount = null;
            pullToRefresh();
        });
        return rootView;
    }

    @Override
    public void onPause() {
        super.onPause();
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.setEnabled(false);
            swipeRefreshLayout.setRefreshing(false);
            swipeRefreshLayout.clearAnimation();
        }
        if (getActivity() != null) {
            InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm != null && getView() != null) {
                imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
            }
        }
    }


    @Override
    public void onCreate(Bundle saveInstance) {
        super.onCreate(saveInstance);
    }


    @Override
    public void onAttach(@NotNull Context context) {
        super.onAttach(context);
        this.context = context;
    }


    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }


    private void manageViewAccounts(APIResponse apiResponse) {
        if (apiResponse != null && apiResponse.getAccounts() != null && apiResponse.getAccounts().size() > 0) {
            if (top_account_container.getVisibility() == View.GONE) {
                top_account_container.setVisibility(View.VISIBLE);
            }
            int previousPosition = accounts.size();
            accounts.addAll(apiResponse.getAccounts());
            accountsHorizontalListAdapter.notifyItemRangeInserted(previousPosition, apiResponse.getAccounts().size());
            if (max_id_accounts == null) {
                max_id_accounts = "0";
            }
            //max_id_accounts needs to work like an offset
            int tootPerPage = sharedpreferences.getInt(Helper.SET_VIDEOS_PER_PAGE, Helper.VIDEOS_PER_PAGE);
            max_id_accounts = String.valueOf(Integer.parseInt(max_id_accounts) + tootPerPage);
        }
    }


    private void manageVIewVideos(APIResponse apiResponse) {
        //hide loaders
        mainLoader.setVisibility(View.GONE);
        nextElementLoader.setVisibility(View.GONE);
        //handle other API error
        if (this.peertubes == null || apiResponse == null || (apiResponse.getError() != null)) {
            if (apiResponse == null)
                Toasty.error(context, context.getString(R.string.toast_error), Toast.LENGTH_LONG).show();
            else {
                Toasty.error(context, apiResponse.getError().getError(), Toast.LENGTH_LONG).show();
            }
            swipeRefreshLayout.setRefreshing(false);
            flag_loading = false;
            return;
        }
        int previousPosition = this.peertubes.size();
        if (max_id == null)
            max_id = "0";
        //max_id needs to work like an offset
        int tootPerPage = sharedpreferences.getInt(Helper.SET_VIDEOS_PER_PAGE, Helper.VIDEOS_PER_PAGE);
        max_id = String.valueOf(Integer.parseInt(max_id) + tootPerPage);
        if (apiResponse.getPeertubes() == null) {
            return;
        }
        this.peertubes.addAll(apiResponse.getPeertubes());
        //If no item were inserted previously the adapter is created
        if (previousPosition == 0) {
            peertubeAdapater = new PeertubeAdapter(this.peertubes, type == TimelineVM.TimelineType.MY_VIDEOS);
            lv_status.setAdapter(peertubeAdapater);
        } else
            peertubeAdapater.notifyItemRangeInserted(previousPosition, apiResponse.getPeertubes().size());
        //remove handlers
        swipeRefreshLayout.setRefreshing(false);
        textviewNoAction.setVisibility(View.GONE);
        if (firstLoad && (apiResponse.getPeertubes() == null || apiResponse.getPeertubes().size() == 0)) {
            textviewNoActionText.setText(R.string.no_video_to_display);
            textviewNoAction.setVisibility(View.VISIBLE);
        }
        flag_loading = false;
        firstLoad = false;
    }


    @Override
    public void onDestroyView() {
        if (lv_status != null) {
            try {
                lv_status.setAdapter(null);
            } catch (Exception ignored) {
            }
        }
        super.onDestroyView();
        rootView = null;
    }

    @Override
    public void onResume() {
        super.onResume();
        swipeRefreshLayout.setEnabled(true);
    }


    public void scrollToTop() {
        if (mLayoutManager != null) {
            mLayoutManager.scrollToPositionWithOffset(0, 0);
        } else if (gLayoutManager != null) {
            gLayoutManager.scrollToPositionWithOffset(0, 0);
        }
    }


    public void pullToRefresh() {
        int size = peertubes.size();
        peertubes.clear();
        peertubes = new ArrayList<>();
        max_id = "0";
        peertubeAdapater.notifyItemRangeRemoved(0, size);
        if (forAccount == null) {
            for (Account account : accounts) {
                account.setSelected(false);
            }
            accountsHorizontalListAdapter.notifyItemRangeRemoved(0, accounts.size());
        }
        loadTimeline("0");
    }

    @Override
    public void click(String forAccount) {
        this.forAccount = forAccount;
        pullToRefresh();
    }

    /**
     * Manage timeline load
     *
     * @param max_id String pagination
     */
    private void loadTimeline(String max_id) {
        if (search_peertube == null) { //Not a Peertube search
            if (type == TimelineVM.TimelineType.USER_VIDEOS) {
                viewModelFeeds.getVideosInChannel(channelId, max_id).observe(this.requireActivity(), this::manageVIewVideos);
            } else {
                viewModelFeeds.getVideos(type, max_id).observe(this.requireActivity(), this::manageVIewVideos);
            }
        } else {
            viewModelSearch.getVideos(max_id, search_peertube).observe(this.requireActivity(), this::manageVIewVideos);
        }
    }

    static class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {

        private int spanCount;
        private int spacing;
        private boolean includeEdge;

        public GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge) {
            this.spanCount = spanCount;
            this.spacing = spacing;
            this.includeEdge = includeEdge;
        }

        @Override
        public void getItemOffsets(@NotNull Rect outRect, @NotNull View view, RecyclerView parent, @NotNull RecyclerView.State state) {
            int position = parent.getChildAdapterPosition(view);
            int column = position % spanCount;

            if (includeEdge) {
                outRect.left = spacing - column * spacing / spanCount;
                outRect.right = (column + 1) * spacing / spanCount;

                if (position < spanCount) {
                    outRect.top = spacing;
                }
                outRect.bottom = spacing;
            } else {
                outRect.left = column * spacing / spanCount;
                outRect.right = spacing - (column + 1) * spacing / spanCount;
                if (position >= spanCount) {
                    outRect.top = spacing;
                }
            }
        }
    }
}
