package com.example.lab1;

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

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

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

public class GradesActivity extends AppCompatActivity {

    private GradeAdapter gradeAdapter;
    private List<Subject> subjects;
    private RecyclerView recyclerView;

    private static final String KEY_GRADES = "key_grades";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_grades);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.mainGrades), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        subjects = new ArrayList<>();
        var subjectArray = getResources().getStringArray(R.array.przedmioty);
        var subjectCount = getIntent().getIntExtra("gradeCount", 5);
        for (var i = 0; i < subjectCount; i++) {
            subjects.add(new Subject(subjectArray[i]));
        }

        if (savedInstanceState != null && savedInstanceState.containsKey(KEY_GRADES)) {
            double[] savedGrades = savedInstanceState.getDoubleArray(KEY_GRADES);
            if (savedGrades != null) {
                for (int i = 0; i < savedGrades.length && i < subjects.size(); i++) {
                    subjects.get(i).grade = savedGrades[i];
                }
            }
        }

        recyclerView = findViewById(R.id.rvListaOcen);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        gradeAdapter = new GradeAdapter(subjects);
        recyclerView.setAdapter(gradeAdapter);

        var calculateButton = findViewById(R.id.btOblicz);
        calculateButton.setOnClickListener(v -> calculateAverage());
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        double[] savedGrades = new double[subjects.size()];
        for (int i = 0; i < subjects.size(); i++) {
            Double grade = subjects.get(i).grade;
            savedGrades[i] = grade;
        }
        outState.putDoubleArray(KEY_GRADES, savedGrades);
    }

    @Override
    public void onBackPressed() {
        Intent resultIntent = new Intent();
        setResult(RESULT_CANCELED, resultIntent);
        super.onBackPressed();
    }

    @Override
    public boolean onSupportNavigateUp() {
        onBackPressed(); // ustawia wynik i kończy aktywność
        return true;
    }

    private void calculateAverage() {
        double sum = 0;
        var items = gradeAdapter.getGradeItems();
        for (Subject item : items) {
            sum += item.grade;
        }

        double average = sum / items.size();
        Intent resultIntent = new Intent();
        resultIntent.putExtra("average", average);
        setResult(RESULT_OK, resultIntent);
        finish();
    }
}
