package com.symbol.elearningapp;

import androidx.appcompat.app.AppCompatActivity;

import android.app.SearchManager;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.Toast;

import com.symbol.elearningapp.service.StudentDetails;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class StudentListActivity extends AppCompatActivity {
    SearchView searchView;
    ListView listView;
    ArrayList<String> list;
    ArrayAdapter<String> adapter;
    HashMap<String, StudentDetails> selectedStudentDetailsHashMap;
    String passedValue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_student_list);

        if (getIntent().getExtras() != null) {
            passedValue = getIntent().getStringExtra("key");

        }
        searchView = (SearchView) findViewById(R.id.searchView);
        listView = (ListView) findViewById(R.id.lv1);
        selectedStudentDetailsHashMap = new HashMap<>();
        list = new ArrayList<>();
       for(StudentDetails details : CommonData.getStudentDetailsList()){
           list.add(details.getStudentName());
       }

        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,list);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener((adapterView, view, i, l) -> {
            if(passedValue.equals("edit")){
                String s = listView.getItemAtPosition(i).toString();
                setStudentList(s);
            }

            // If you want to close the adapter
        });

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {

                if(list.contains(query)){
                    adapter.getFilter().filter(query);
                }else{
                    Toast.makeText(StudentListActivity.this, "No Match found",Toast.LENGTH_LONG).show();
                }
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                    adapter.getFilter().filter(newText);
                return false;
            }
        });
        if(passedValue.equals("edit")){
            if(CommonData.getClassDetails().get(CommonData.getSelectedIndex()).getListStd() != null){
                for(StudentDetails studentDetails :CommonData.getClassDetails().get(CommonData.getSelectedIndex()).getListStd()){
                    selectedStudentDetailsHashMap.put(studentDetails.getStudentName(),studentDetails);
                }
            }

        }
    }

    public void setStudentList(String name){
        boolean alreadyNotAdded = true;
        if (CommonData.getSelectedStudentDetailsHashMap()!=null) {

            for(Map.Entry<String, StudentDetails> entry : CommonData.getSelectedStudentDetailsHashMap().entrySet()) {
                String key = entry.getKey();
                if(name.equals(key)){
                    alreadyNotAdded = false;
                }
            }
        }
        else if(selectedStudentDetailsHashMap!=null){
            for(Map.Entry<String, StudentDetails> entry : selectedStudentDetailsHashMap.entrySet()) {
                String key = entry.getKey();
                if(name.equals(key)){
                    alreadyNotAdded = false;
                }
            }

        }

        if(alreadyNotAdded)
        {
            for(Map.Entry<String, StudentDetails> entry : CommonData.getStudentDetailsHashMap().entrySet()) {
                String key = entry.getKey();
                StudentDetails value = entry.getValue();
                if(name.equals(key)){
                    selectedStudentDetailsHashMap.put(key,value);
                }
            }
            Toast.makeText(StudentListActivity.this, "Added sucessfully",Toast.LENGTH_LONG).show();
        }
        else
        {
            Toast.makeText(StudentListActivity.this, "Already added",Toast.LENGTH_LONG).show();
        }
        }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if(passedValue.equals("edit")){
            CommonData.setSelectedStudentDetailsHashMap(selectedStudentDetailsHashMap);
        }

    }
}