package com.example.leudaemialikeme.Fragment;

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

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

import com.example.leudaemialikeme.Activity.InfoActivity;
import com.example.leudaemialikeme.Activity.MdctRmdActivity;
import com.example.leudaemialikeme.Activity.SearchActivity;
import com.example.leudaemialikeme.R;
import com.example.leudaemialikeme.Model.Recnews;
import com.example.leudaemialikeme.Adapter.RecnewsAdapter;

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

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link IndexFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class IndexFragment extends Fragment {

    private List<Recnews> recNews=new ArrayList<>();

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;


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

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment IndexFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static IndexFragment newInstance(String param1, String param2) {
        IndexFragment fragment = new IndexFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_index, container, false);
        initRecNews();
        RecyclerView recyclerView=(RecyclerView) view.findViewById(R.id.recommend_news);
        LinearLayoutManager layoutManager=new LinearLayoutManager(this.getActivity());
        recyclerView.setLayoutManager(layoutManager);
        RecnewsAdapter recnewsAdapter=new RecnewsAdapter(recNews);
        recyclerView.setAdapter(recnewsAdapter);

        initClick(view);

        return view;

    }

    private void initClick(View view){
        LinearLayout searchLinear=view.findViewById(R.id.search_linear);
        LinearLayout recLinear=view.findViewById(R.id.rec_linear);
        LinearLayout kepuLinear=view.findViewById(R.id.kepu_linear);
        LinearLayout clockLinear=view.findViewById(R.id.clock_linear);
        LinearLayout companyLinear=view.findViewById(R.id.company);
        LinearLayout tenLinear=view.findViewById(R.id.ten);
        LinearLayout governmentLinear=view.findViewById(R.id.government);
        LinearLayout charity=view.findViewById(R.id.charity);
        LinearLayout all=view.findViewById(R.id.all);

        searchLinear.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getContext(), SearchActivity.class);
                startActivity(intent);
            }
        });
        recLinear.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {

            }
        });
        kepuLinear.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {

            }
        });
        clockLinear.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getContext(), MdctRmdActivity.class);
                startActivity(intent);            }
        });

        companyLinear.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getContext(), InfoActivity.class);
                int data=0;
                intent.putExtra("info_type",data);
                startActivity(intent);            }
        });

        tenLinear.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getContext(), InfoActivity.class);
                int data=1;
                intent.putExtra("info_type",data);
                startActivity(intent);            }
        });

        governmentLinear.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getContext(), InfoActivity.class);
                int data=2;
                intent.putExtra("info_type",data);
                startActivity(intent);            }
        });

        charity.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getContext(), InfoActivity.class);
                int data=3;
                intent.putExtra("info_type",data);
                startActivity(intent);            }
        });

        all.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getContext(), InfoActivity.class);
                int data=4;
                intent.putExtra("info_type",data);
                startActivity(intent);
            }
        });


    }


    //int imageNum,String theme,String intro,int visNum, String time
    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);
        }
    }
}