package com.symbol.elearningapp;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

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 com.symbol.elearningapp.service.StudentDetails;
import com.symbol.elearningapp.service.User;

import java.util.HashMap;
import java.util.List;

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

public class HomeActivity extends AppCompatActivity {
    LinearLayout ll1, ll2, ll3, ll4;
    ImageView iv1, iv2, iv3, iv4;
    Button bt1, bt2, bt3, bt4;
    String type = "student";
    AppPreference appPreference;
    ServiceApi serviceApi;
    ProgressDialog progress;

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

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

        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 1);
            Toast.makeText(this, " Please Grant Permission for Camera", Toast.LENGTH_SHORT).show();
        } else if (ActivityCompat.checkSelfPermission(this, Manifest.permission.MODIFY_AUDIO_SETTINGS) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.MODIFY_AUDIO_SETTINGS}, 1);
            Toast.makeText(this, " Please Grant Permission for Camera", Toast.LENGTH_SHORT).show();
        }
        ll1 = findViewById(R.id.llbutton1);
        ll2 = findViewById(R.id.llbutton2);
        ll3 = findViewById(R.id.llbutton3);
        ll4 = findViewById(R.id.llbutton4);
        iv1 = findViewById(R.id.ivbutton1);
        iv2 = findViewById(R.id.ivbutton2);
        iv3 = findViewById(R.id.ivbutton3);
        iv4 = findViewById(R.id.ivbutton4);
        bt1 = findViewById(R.id.ttbutton1);
        bt2 = findViewById(R.id.ttbutton2);
        bt3 = findViewById(R.id.ttbutton3);
        bt4 = findViewById(R.id.ttbutton4);


        populateUI();


    }

    public void getClassdetails() {
        progress = new ProgressDialog(this);
        progress.setTitle("Please wait");
        progress.setCancelable(false);
        progress.show();
        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());
                    progress.dismiss();
                    Intent intent = new Intent(HomeActivity.this, ClassDetailActivty.class);
                    startActivity(intent);
                } else {
                    appPreference.showToast("Server Error occured!!");
                }
            }

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

        });
    }

    public void getClassjoindetails() {
        progress = new ProgressDialog(this);
        progress.setTitle("Please wait");
        progress.setCancelable(false);
        progress.show();
        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());
                    progress.dismiss();
                    Intent intent = new Intent(HomeActivity.this, JoinClassActivty.class);
                    startActivity(intent);
                } else {
                    appPreference.showToast("Server Error occured!!");
                }
            }

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

        });
    }

    public void getAttendanceActivity() {
        progress = new ProgressDialog(this);
        progress.setTitle("Please wait");
        progress.setCancelable(false);
        progress.show();
        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());
                    progress.dismiss();
                    Intent intent = new Intent(HomeActivity.this, AttendenceActivity.class);
                    startActivity(intent);
                } else {
                    appPreference.showToast("Server Error occured!!");
                }
            }

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

        });
    }

    public void getStudentList() {
        progress = new ProgressDialog(this);
        progress.setTitle("Please wait");
        progress.setCancelable(false);
        progress.show();
        Call<List<StudentDetails>> userCall = serviceApi.getStudents("class/students");
        userCall.enqueue(new Callback<List<StudentDetails>>() {
            @Override
            public void onResponse(Call<List<StudentDetails>> call, Response<List<StudentDetails>> response) {

                if (response.body()!=null)
                {
                    CommonData.setStudentDetailsList(response.body());
                    CommonData.setStudentDetailsHashMap(new HashMap<>());
                    CommonData.setStudentDetailsHashMap(new HashMap<>());
                    for(StudentDetails details : CommonData.getStudentDetailsList()){
                        CommonData.getStudentDetailsHashMap().put(details.getStudentName(),details);
                    }

                    progress.dismiss();
                    Intent intent = new Intent(HomeActivity.this, StudentListActivity.class);
                    intent.putExtra("key", "home");
                    startActivity(intent);
                }
                else
                {
                    appPreference.showToast("Server Error occured!!");
                }
            }

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

        });
    }

    public void populateUI() {
        if (type.equals("student")) {
            ll1.setVisibility(View.VISIBLE);
            ll2.setVisibility(View.VISIBLE);
            ll3.setVisibility(View.VISIBLE);
            ll4.setVisibility(View.INVISIBLE);
            iv1.setBackgroundResource(R.drawable.classlogo);
            iv2.setBackgroundResource(R.drawable.attendancelogo);
            iv3.setBackgroundResource(R.drawable.joinlogo);
            bt1.setText("Class Details");
            bt2.setText("Attendance");
            bt3.setText("Join");

            bt1.setOnClickListener(v -> {
                getClassdetails();
            });

            bt2.setOnClickListener(v -> {
                getAttendanceActivity();
            });

            bt3.setOnClickListener(v -> {
                getClassjoindetails();
            });
        } else {
            ll1.setVisibility(View.VISIBLE);
            ll2.setVisibility(View.VISIBLE);
            ll3.setVisibility(View.VISIBLE);
            ll4.setVisibility(View.INVISIBLE);
            iv1.setBackgroundResource(R.drawable.classlogo);
            iv2.setBackgroundResource(R.drawable.attendancelogo);
            iv3.setBackgroundResource(R.drawable.joinlogo);
            bt1.setText("Class Details");
            bt2.setText("Students");
            bt3.setText("Start Session");

            bt1.setOnClickListener(v -> {
                getClassdetails();

            });

            bt2.setOnClickListener(v -> {
                getStudentList();
            });
            bt3.setOnClickListener(v -> {
                getClassjoindetails();
            });

        }
    }


}