package org.ntlab.acanthus_client.views.main_menu_ui.home;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import org.ntlab.acanthus_client.Acanthus;
import org.ntlab.acanthus_client.R;
import org.ntlab.acanthus_client.databinding.FragmentHomeBinding;
import org.ntlab.acanthus_client.entities.Animation;
import org.ntlab.acanthus_client.entities.AnimationJson;
import org.ntlab.acanthus_client.views.animation.AnimationActivity;
import org.ntlab.acanthus_client.views.paint.PaintActivity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;

public class HomeFragment extends Fragment {

    private HomeViewModel homeViewModel;
    private FragmentHomeBinding binding;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        homeViewModel =
                new ViewModelProvider(this).get(HomeViewModel.class);

        binding = FragmentHomeBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        ListView listView = binding.animationView;

//        homeViewModel.updateGallery();
//
//        final TextView textView = binding.textHome;
//        homeViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
//            @Override
//            public void onChanged(@Nullable String s) {
//
//                textView.setText(s);
//            }
//        });
        return root;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.d("CreatedActivity", "EditFragment");

        Acanthus acanthus = (Acanthus) getActivity().getApplication();
        Integer uid = 1;
        String token = "abc0";

        ListView listView = (ListView) getActivity().findViewById(R.id.animation_view);

        homeViewModel.updateGallery(acanthus);

        homeViewModel.getAnimationJson().observe(getViewLifecycleOwner(), new Observer<Collection<AnimationJson>>() {
            @Override
            public void onChanged(Collection<AnimationJson> animationJsons) {
                Collection<AnimationJson> animationJsonList = homeViewModel.getGallery();
                //ソート用宣言
                ArrayList<String> createDateList = new ArrayList<>();
                ArrayList<AnimationJson> animationJsonSortList =new ArrayList<>();

                //ソート処理
                for (AnimationJson animationJson : animationJsonList){
                    createDateList.add(animationJson.getCreatedDate());
                }
                Collections.sort(createDateList);
                Collections.reverse(createDateList);
                for (String createDate : createDateList){
                    for (AnimationJson animationJson : animationJsonList){
                        if (createDate == animationJson.getCreatedDate()){
                            animationJsonSortList.add(animationJson);
                        }
                    }
                }
                BaseAdapter adapter = new ListAnimationViewAdapter(getActivity(), R.layout.animation_home, animationJsonSortList);
                listView.setAdapter(adapter);

                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
//                        homeViewModel.setCurrentAnimation(animationJsonSortList.get(position).getAid(), acanthus);
//                        transitionAnimationActivity(animationJsonSortList.get(position));
                        try {
                            homeViewModel.setCurrentAnimation(animationJsonSortList.get(position).getAid(), acanthus);
                            transitionAnimationActivity(animationJsonSortList.get(position));
                        } catch (IOException e) {
                            e.printStackTrace();
                        }


                    }
                });
            }
        });

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

    private void transitionAnimationActivity(AnimationJson animationJson) {
        Acanthus acanthus = (Acanthus) getActivity().getApplication();
        Intent intent = new Intent(acanthus, AnimationActivity.class);
        intent.putExtra("AnimationJsonData", animationJson);
        startActivity(intent);
    }
}