package com.hh.holyolm;

import android.content.Intent;
import android.os.Bundle;

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

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

import com.hh.holyolm.adapter.CommomRvAdapter;
import com.hh.holyolm.adapter.CommomRvViewHolder;
import com.hh.holyolm.model.OptCardModel;
import com.hh.holyolm.model.TaskModel;
import com.hh.holyolm.util.XmlUtil;

import java.io.File;
import java.util.List;

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




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


    public static TaskListFragment newInstance(String param1, String param2) {
        TaskListFragment fragment = new TaskListFragment();

        return fragment;
    }

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

    }

    View root;
    MainActivity parent;
    File taskDir;
    List<TaskModel> taskList;
    RecyclerView mRecyclerView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        root= inflater.inflate(R.layout.fragment_task_list, container, false);
        parent=(MainActivity) getActivity();
        taskDir=parent.getTaskDir();

        mRecyclerView = (RecyclerView) root.findViewById(R.id.rv_task);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        loadData();

        root.findViewById(R.id.btnRefresh).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loadData();
            }
        });

        return root;
    }

    private void loadData(){
        taskList= XmlUtil.ReadTaskList(taskDir);

        CommomRvAdapter<TaskModel> adapter = new CommomRvAdapter<TaskModel>(getContext(), taskList, R.layout.list_item_task) {
            @Override
            protected void fillData(CommomRvViewHolder holder, int position, TaskModel model) {
                holder.setText(R.id.tv_task_num, model.getTaskNum());
                holder.setText(R.id.tv_model, model.getPlaneModel());
                holder.setText(R.id.tv_plane_num, model.getPlaneNum());
                holder.setText(R.id.tv_card_number, model.getCardNum());
                holder.setText(R.id.tv_status, model.getStatus());
                holder.setText(R.id.tv_intime, model.getIntime());
                holder.setText(R.id.tv_name, model.getCardName());
            }
        };
        adapter.setOnItemClickListener((view, position) -> {
            //String pdfFile=libsDir.getAbsolutePath()+File.separator+"action"+File.separator+ cardModelList.get(position).getPdfFile();
            //showPDF(pdfFile);
            Intent intent=new Intent(getContext(), ExecTaskActivity.class);
            intent.putExtra("PkgPath",taskList.get(position).getPackagePath());
            intent.putExtra("Model",ExecTaskActivity.USE_MODEL_EXEC);
            startActivity(intent);
        });
        mRecyclerView.setAdapter(adapter);
    }
}