package com.frutch.artogo.Fragments;

import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

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

import com.frutch.artogo.APIDAO.Models.Article;
import com.frutch.artogo.APIDAO.Models.Artist;
import com.frutch.artogo.APIDAO.DAO;
import com.frutch.artogo.Adapters.ExhibitionArticleAdapter;
import com.frutch.artogo.R;

/**
 * Created by paris guillaume.
 * This fragment manage one artist
 * It extends Fragment, which is the basic fragment : android.support.v4.app.Fragment
 */

public class ArtistFragment extends Fragment {
    private Artist artist;
    private List<Uri> audioList;
    private List<Uri> photoList;
    private List<Uri> videoList;
    private ImageView imageMaximize;
    private LinearLayout layoutMaximize;
    private boolean openDesc;
    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        final CoordinatorLayout layout = (CoordinatorLayout) inflater.inflate(R.layout.fragment_artist, container, false);

        artist = (Artist) getArguments().getSerializable("obj");
        openDesc = false;
        imageMaximize = (ImageView) layout.findViewById(R.id.image_maximise);
        layoutMaximize = (LinearLayout) layout.findViewById(R.id.layout_image_max);
        TextView artistName = (TextView) layout.findViewById(R.id.artist_name);
        final TextView artistDesc = (TextView) layout.findViewById(R.id.artist_desc);
        ImageView artistBack = (ImageView) layout.findViewById(R.id.background_artist);

        artistName.setText(artist.getName());
        artistDesc.setText(artist.getAboutMe());
        if (artist.getResource() != null && artist.getResource().getUri() != null) {
            artistBack.setImageURI(Uri.parse(artist.getResource().getUri()));
        }
        artistBack.setImageURI(Uri.parse(artist.getResource().getUri()));
        layout.findViewById(R.id.button_expand).setOnClickListener(new View.OnClickListener() {
            LinearLayout layoutExpand = ((LinearLayout) layout.findViewById(R.id.layout_expand));
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) layoutExpand.getLayoutParams();

            @Override
            public void onClick(View v) {
                if (params.weight == 0f) {
                    params.weight = 0.6f;
                    artistDesc.setMaxLines(2);
                    v.setBackgroundResource(R.mipmap.ic_keyboard_arrow_down_white_24dp);
                } else {
                    params.weight = 0f;
                    artistDesc.setMaxLines(Integer.MAX_VALUE);
                    v.setBackgroundResource(R.mipmap.ic_keyboard_arrow_up_white_24dp);
                }
            }
        });
        DAO dao = DAO.getInstance(getContext());
        ArrayList<Article> articleList = dao.getArtistArticles(artist.getId());

        mRecyclerView = (RecyclerView) layout.findViewById(R.id.recycler_view_articles);

        // Use a linear layout manager
        mLayoutManager = new LinearLayoutManager(getContext());
        mRecyclerView.setLayoutManager(mLayoutManager);

        // Specify an adapter (see also next example)
        mAdapter = new ExhibitionArticleAdapter(getContext(), articleList);
        mRecyclerView.setAdapter(mAdapter);

        return layout;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        menu.clear();
    }
}
