package com.application.tak.takapplication.student.task;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.application.tak.takapplication.R;
import com.application.tak.takapplication.adapters.RVAdapterAllTaskStudent;
import com.application.tak.takapplication.data_access.Config;
import com.application.tak.takapplication.data_access.GetAllStudentTasks;
import com.application.tak.takapplication.data_list.AllTaskListStudent;
import com.application.tak.takapplication.data_model.Task_V;
import com.application.tak.takapplication.data_model.User;
import com.application.tak.takapplication.interfaces.OnDBRequestFinished;
import com.application.tak.takapplication.local_db.LocalDB;
import com.application.tak.takapplication.services.BackgroundService;

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

/**
 * Created by Aleksandra on 04.09.2017.
 */
public class actStudentAllTask extends Fragment {


    private RecyclerView recyclerview;
    private List<AllTaskListStudent> memberList;

    //User client = new User();
    Task_V task;
   GetAllStudentTasks tasks;
   Context ctx;

    public actStudentAllTask()
    {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {


        View view = inflater.inflate(R.layout.act_student_alltask, container, false);
        setRetainInstance(true);
        ctx = getContext();

        memberList = new ArrayList<AllTaskListStudent>();
        Button google_map = (Button) view.findViewById(R.id.btnAdresMap);

        recyclerview = (RecyclerView) view.findViewById(R.id.recyclerview_alltask);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        recyclerview.setLayoutManager(layoutManager);

        GetTasks();



        return view;
    }

    public void GetTasks()
    {
        LocalDB local = new LocalDB(ctx);
        tasks = new GetAllStudentTasks(ctx,Config.LoggedInStudent);

        tasks.setDBRequestFinishedListener(new OnDBRequestFinished() {

            @Override
            public void onDBRequestFinished() {
                if (tasks._tasks != null) {
                    Config.StudentTask = tasks._tasks;
                    if(memberList != null)
                    {
                    memberList.clear();
                    local.ClearAllNotificationsForUser(Config.LoggedInStudent.get_Id());
                    for (Task_V task :  Config.StudentTask ) {

                        AllTaskListStudent member = new AllTaskListStudent(task);
                        memberList.add(member);

                        local.AddNotificationRead(Config.LoggedInStudent.get_Id(),task.get_Id());
                    }
                    }
                }
                RVAdapterAllTaskStudent adapter = new RVAdapterAllTaskStudent(memberList, getActivity());
                if(recyclerview != null && adapter != null)
                    recyclerview.setAdapter(adapter);

                getActivity().startService(new Intent(ctx, BackgroundService.class));

            }

        });
    }

}
