package org.mercury_im.messenger.ui.account;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton;

import org.mercury_im.messenger.R;
import org.mercury_im.messenger.entity.Account;
import org.mercury_im.messenger.xmpp.MercuryConnection;
import org.mercury_im.messenger.xmpp.state.ConnectionState;

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

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * A fragment representing a list of Items.
 * <p/>
 * Activities containing this fragment MUST implement the {@link OnAccountListItemClickListener}
 * interface.
 */
public class AccountsFragment extends Fragment {

    private OnAccountListItemClickListener accountClickListener;

    AccountsViewModel viewModel;

    private AccountsRecyclerViewAdapter adapter;

    @BindView(R.id.list)
    RecyclerView recyclerView;

    @BindView(R.id.fab)
    ExtendedFloatingActionButton fab;

    public AccountsFragment() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewModel = new ViewModelProvider(this).get(AccountsViewModel.class);
        this.adapter = new AccountsRecyclerViewAdapter(viewModel, accountClickListener);
    }

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

    private void observeViewModel() {
        viewModel.getConnectionPool().observe(this, pool -> {
            Collection<ConnectionState> connectionStateList = pool.getConnectionStates().values();
            List<MercuryConnection> connections = new ArrayList<>(connectionStateList.size());
            for (ConnectionState state : connectionStateList) {
                connections.add(state.getConnection());
            }
            adapter.setValues(connections);
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_account_list, container, false);
        ButterKnife.bind(this, view);

        Context context = view.getContext();
        fab.setOnClickListener(v -> startActivity(new Intent(context, LoginActivity.class)));
        recyclerView.setLayoutManager(new LinearLayoutManager(context));

        recyclerView.setAdapter(adapter);

        return view;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnAccountListItemClickListener) {
            accountClickListener = (OnAccountListItemClickListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnAccountListItemClickListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        accountClickListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p/>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnAccountListItemClickListener {
        void onAccountListItemClick(Account item);

        void onAccountListItemLongClick(Account item);
    }
}
