package com.example.leudaemialikeme.Fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

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

import com.example.leudaemialikeme.Adapter.RecnewsAdapter;
import com.example.leudaemialikeme.R;
import com.example.leudaemialikeme.Model.Recnews;

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

public class InvitationChannelFragment extends Fragment {
    private static final String ARG_CATEGORY_TITLE = "category_title";
    private String invitationCategoryTitle = "Default";
    private TextView mTitleField;
    private List<Recnews> recNews=new ArrayList<>();

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

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

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view;
        if(invitationCategoryTitle.equals("全部")){
            view = inflater.inflate(R.layout.recyclerview,container,false);
            RecyclerView recyclerView = (RecyclerView)view.findViewById(R.id.list_community);
            LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
            recyclerView.setLayoutManager(layoutManager);
            RecnewsAdapter adapter = new RecnewsAdapter(recNews);
            recyclerView.setAdapter(adapter);
        }
        else {
            view = inflater.inflate(R.layout.fragment_invitation_channel, container, false);
            mTitleField = (TextView)view.findViewById(R.id.invitationCategoryTitle);
            mTitleField.setText(invitationCategoryTitle);
        }
        return view;
    }

    private void initRecNews(){
        for(int i=0;i<2;i++){
            Recnews news1=new Recnews(R.drawable.rec_news1,
                    "儿童急性B淋巴细胞白血病,除了传统疗法还有哪些前沿治疗方法呢？",
                    "急性淋巴细胞白血病（ALL）是儿童最常见的恶性肿瘤，其中约85%为……"
                    ,102,"2021-11-6 09:19:54"
            );
            recNews.add(news1);
            Recnews news2=new Recnews(R.drawable.rec_news2,
                    "儿童急性B淋巴细胞白血病,除了传统疗法还有哪些前沿治疗方法呢？",
                    "急性淋巴细胞白血病（ALL）是儿童最常见的恶性肿瘤，其中约85%为……"
                    ,100,"2021-12-6 03:19:54"
            );
            recNews.add(news2);
            Recnews news3=new Recnews(R.drawable.rec_news3,
                    "儿童急性B淋巴细胞白血病,除了传统疗法还有哪些前沿治疗方法呢？",
                    "急性淋巴细胞白血病（ALL）是儿童最常见的恶性肿瘤，其中约85%为……"
                    ,100,"2021-12-6 03:19:54"
            );
            recNews.add(news3);
            Recnews news4=new Recnews(R.drawable.rec_news4,
                    "儿童急性B淋巴细胞白血病,除了传统疗法还有哪些前沿治疗方法呢？",
                    "急性淋巴细胞白血病（ALL）是儿童最常见的恶性肿瘤，其中约85%为……"
                    ,100,"2021-12-6 03:19:54"
            );
            recNews.add(news4);
        }
    }
}