package com.symbol.elearningapp;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import com.symbol.elearningapp.Utils.AppPreference;
import com.symbol.elearningapp.Utils.Constant;
import com.symbol.elearningapp.service.ChartData;
import com.symbol.elearningapp.service.ClassDetail;
import com.symbol.elearningapp.service.RetrofitClient;
import com.symbol.elearningapp.service.ServiceApi;

import org.eazegraph.lib.charts.PieChart;
import org.eazegraph.lib.models.PieModel;

import java.util.Calendar;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class AttendenceActivity extends AppCompatActivity {
    PieChart pieChart;
    Spinner dropdown;
    TextView tvsubject,tvTotal,tvAbsent,tvPresent;
    ServiceApi serviceApi;
    ProgressDialog progress;
    AppPreference appPreference;

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

        appPreference = new AppPreference(this);
        serviceApi = RetrofitClient.getApiClient(Constant.baseUrl.BASE_URL).create(ServiceApi.class);

        pieChart = findViewById(R.id.piechart);
        dropdown = findViewById(R.id.spinner1);
        tvsubject =findViewById(R.id.tvsubject);
        tvTotal = findViewById(R.id.tvTotal);
        tvAbsent = findViewById(R.id.tvAbsent);
        tvPresent = findViewById(R.id.tvPresent);

        List<ClassDetail> details= CommonData.getClassDetails();
        String[] items = new String[details.size()];
        for(int i = 0;i < details.size();i++)
        {
            items[i] = details.get(i).getSubject();
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, items);
        dropdown.setAdapter(adapter);

        dropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String subjectName = dropdown.getItemAtPosition(position).toString();
                updateClassdetails(subjectName);

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }

    private void createChart(int present,int absent){
        pieChart.clearChart();
        pieChart.addPieSlice(
                new PieModel(
                        "Present",
                        present,
                        Color.parseColor("#66BB6A")));
        pieChart.addPieSlice(
                new PieModel(
                        "Absent",
                        absent,
                        Color.parseColor("#EF5350")));


        pieChart.startAnimation();

    }
    public void updateClassdetails(String subject) {
        progress = new ProgressDialog(this);
        progress.setTitle("Please wait");
        progress.setCancelable(false);
        progress.show();

            Call<ChartData> userCall = serviceApi.getAttendance("atd/"+CommonData.getId()+"/"+subject);
//        Call<ChartData> userCall = serviceApi.getAttendance("atd/29/"+subject);
            userCall.enqueue(new Callback<ChartData>() {

                @Override
                public void onResponse(Call<ChartData> call, Response<ChartData> response) {
                    if(response.body() != null)
                    {
                        progress.dismiss();
                        int total = response.body().getReal();
                        int present =response.body().getDetected();
                        int absent = total-present;
                        createChart(present,absent);
                        tvsubject.setText(subject);
                        tvAbsent.setText(""+absent);
                        tvTotal.setText(""+total);
                        tvPresent.setText(""+present);

                    }
                    else
                    {
                        progress.dismiss();
                        appPreference.showToast("Server Error occured!!");
                    }

                }

                @Override
                public void onFailure(Call<ChartData> call, Throwable t) {
                    progress.dismiss();
                    appPreference.showToast("Server Error occured!!");
                }
            });


    }
    }


