package com.example.leudaemialikeme.Fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.leudaemialikeme.Adapter.CollectAdapter;
import com.example.leudaemialikeme.Model.Collect;
import com.example.leudaemialikeme.R;

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

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * to handle interaction events.
 * Use the {@link NewsCollectFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class NewsCollectFragment extends Fragment {
    private static final String ARG_CATEGORY_TITLE = "category_title";
    private String collectCategoryTitle = "Default";
    private List<Collect> collectList = new ArrayList<>();
    private SwipeRefreshLayout mSwipeRefreshLayout;

    public NewsCollectFragment() {
        // Required empty public constructor
    }

    public static NewsCollectFragment newInstance(String collectCategoryTitle) {
        Bundle args = new Bundle();
        args.putString(ARG_CATEGORY_TITLE, collectCategoryTitle);
        NewsCollectFragment fragment = new NewsCollectFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            collectCategoryTitle = (String) getArguments().getString(ARG_CATEGORY_TITLE);
        }catch (java.lang.NullPointerException e)
        {
            System.out.println("TestFragment getArg error!");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_news_collect, container, false);
        getData();//获得数据
        RecyclerView recycleView = (RecyclerView) view.findViewById(R.id.APNList);//获得视图
        LinearLayoutManager layoutManager;
        layoutManager = new LinearLayoutManager(getActivity());
        recycleView.setLayoutManager(layoutManager);//建立线性布局
        final CollectAdapter adapter = new CollectAdapter(collectList);//创建适配器
        recycleView.setItemAnimator(new DefaultItemAnimator());//设置动画效果
        recycleView.setAdapter(adapter);
        mSwipeRefreshLayout = view.findViewById(R.id.refresh_layout);
//        mTextView = view.findViewById(R.id.tv_pull_down_refresh);
        handleDownPullUpdate();

        return view;
    }

    private void handleDownPullUpdate() {
        mSwipeRefreshLayout.setEnabled(true);
        mSwipeRefreshLayout.setColorSchemeResources(R.color.colorAccent,R.color.colorPrimary,R.color.light_grey);
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //不再显示提示正在刷新和刷新成功
//                mTextView.setText("正在刷新...");
                //被刷新时的操作
                //更新UI
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        //更新成功后设置UI，停止更新
//                        mTextView.setText("刷新成功！！！");
                        mSwipeRefreshLayout.setRefreshing(false);
                    }
                },3000);
            }
        });
    }

    private void getData(){
        Collect collect1 = new Collect("临时测算",R.mipmap.img_my_person,"病因很重要","加油加油"
        ,R.mipmap.img_search,"12浏览");
        collectList.add(collect1);
        Collect collect2 = new Collect("临时测算",R.mipmap.img_my_person,"病因很重要","加油加油"
                ,R.mipmap.img_search,"12浏览");
        collectList.add(collect2);
    }
}
