package com.symbol.elearningapp;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.core.content.ContextCompat;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.TextView;

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

import java.sql.SQLOutput;

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

public class LoginActivity extends AppCompatActivity {
    Button loginButton;
    AppCompatImageView ivStudent, ivTutor;
    TextView tvPWD, tvUserName;
    ServiceApi serviceApi;
    AppPreference appPreference;
    String type = "student";
    ProgressDialog progress;

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

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

        loginButton = findViewById(R.id.btn_login);
        ivStudent = findViewById(R.id.iv_student);
        ivTutor = findViewById(R.id.iv_teacher);
        tvPWD = findViewById(R.id.tvPWD);
        tvUserName =  findViewById(R.id.tvUserName);

        ivStudent.setOnClickListener(View -> {
            type = "student";
            ivStudent.setBackground(ContextCompat.getDrawable(this, R.drawable.darkgrey_back_curved));
            ivTutor.setBackground(ContextCompat.getDrawable(this, R.drawable.grey_back_curved));
        });

        ivTutor.setOnClickListener(View -> {
            type = "tutor";
            ivTutor.setBackground(ContextCompat.getDrawable(this, R.drawable.darkgrey_back_curved));
            ivStudent.setBackground(ContextCompat.getDrawable(this, R.drawable.grey_back_curved));
        });

        loginButton.setOnClickListener(View -> {
            userLogin();
        });

    }

    public void userLogin() {
        progress = new ProgressDialog(this);
        progress.setTitle("Please wait");
        progress.setCancelable(false);
        progress.show();

        String userName = tvUserName.getText().toString();
        String Password = tvPWD.getText().toString();

        if (TextUtils.isEmpty(userName)) {
           appPreference.showToast("Username is required.");
        } else if (TextUtils.isEmpty(Password)) {
           appPreference.showToast("Password required");
        } else {
//            Call<String> userCall = serviceApi.login(new User(type, "bandara.p@gmail.com", "Bandara@123"));
            Call<String> userCall = serviceApi.login(new User(type, userName,Password));
            userCall.enqueue(new Callback<String>() {
                @Override
                public void onResponse(Call<String> call, Response<String> response) {
                    String responseString = response.body().toString();
                    String[] splitStr = responseString.split("\\s+");
                    if (splitStr[0].equals("student")) {
                        CommonData.setType("student");
                        CommonData.setId(splitStr[1]);
                        Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
                        startActivity(intent);
                        progress.dismiss();
                        tvUserName.setText("");
                        tvPWD.setText("");
                    }
                    else if (splitStr[0].equals("tutor")) {
                        CommonData.setType("tutor");
                        CommonData.setId(splitStr[1]);
                        Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
                        startActivity(intent);
                        progress.dismiss();
                        tvUserName.setText("");
                        tvPWD.setText("");
                    }
                    else {
                        appPreference.showToast(response.body());
                    }
                }
                @Override
                public void onFailure(Call<String> call, Throwable t) {
                       appPreference.showToast("Server Error.!");
                       progress.dismiss();
                }
            });
        }


    }
}