package com.symbol.elearningapp;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

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

import java.util.List;

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

public class JoinClassActivty extends AppCompatActivity implements ClassAdapter.ClickListener {
    AppPreference appPreference;
    Button addBtn;
    ServiceApi serviceApi;
    RecyclerView recyclerView;
    ClassAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_class_detail_activty);
        appPreference = new AppPreference(this);
        addBtn = findViewById(R.id.btn_add);
        serviceApi = RetrofitClient.getApiClient(Constant.baseUrl.BASE_URL).create(ServiceApi.class);

            addBtn.setVisibility(View.INVISIBLE);

        addBtn.setOnClickListener(v -> {
            Intent intent = new Intent(JoinClassActivty.this, ClassAddActivity.class);
            startActivity(intent);
            this.finish();
        });

        recyclerView = (RecyclerView) findViewById(R.id.rvDetails);
         adapter = new ClassAdapter(CommonData.getClassDetails(), this, this);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void onItemClicked(int position) {

            CommonData.setSelectedIndex(position);
            Intent intent = new Intent(JoinClassActivty.this, JoinActivity.class);
            startActivity(intent);
            this.finish();



    }
    public void getClassdetails(){
        Call<List<ClassDetail>> userCall = serviceApi.classDetails();
        userCall.enqueue(new Callback<List<ClassDetail>>() {
            @Override
            public void onResponse(Call<List<ClassDetail>> call, Response<List<ClassDetail>> response) {

                if (!response.body().equals("")) {

                    CommonData.setClassDetails(response.body());
                }
                else {
                    appPreference.showToast("Server Error occured!!");
                }
            }

            @Override
            public void onFailure(Call<List<ClassDetail>> call, Throwable t) {
                appPreference.showToast(t.getMessage());
            }

        });
    }

    @Override
    protected void onResume() {
        super.onResume();
//        getClassdetails();
//        adapter.notifyDataSetChanged();
    }

}