package com.kaviz.heal.Activities.Authentication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.kaviz.heal.Activities.HomeActivity;
import com.kaviz.heal.Model.User;
import com.kaviz.heal.databinding.ActivityMainBinding;

import java.util.Objects;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;

    //firebase objects
    private FirebaseAuth auth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        makeFullScreen();
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        //initialize objects
        auth = FirebaseAuth.getInstance();

        //for play animation
        setAnimation();

       //login button process
        binding.loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //for login user validation
                loginProcess();

            }
        });

        //for navigate registration activity
        binding.newUserText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                startActivity(new Intent(MainActivity.this, RegisterActivity.class));

            }
        });

    }

    //method for login user validation
    private void loginProcess() {

        String email = binding.emailText.getText().toString();
        String password = binding.passwordText.getText().toString();

        if (email.isEmpty()) {

            binding.emailText.setError("Enter Email");

        } else if (password.isEmpty()) {

            binding.passwordText.setError("Enter Password");

        } else {

            binding.progressBar.setVisibility(View.VISIBLE);
            binding.loginButton.setEnabled(false);
            //for login user
            loginUser(new User(email, password));

        }

    }

    //method for login user
    private void loginUser(User user) {

        auth.signInWithEmailAndPassword(user.getEmail(), user.getPassword())
                .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {

                        if (task.isSuccessful()) {

                            showToast("Successfully Logged In!");
                            Intent intent = new Intent(MainActivity.this, HomeActivity.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            startActivity(intent);

                        } else {

                            showToast("Error - "+ Objects.requireNonNull(task.getException()).getMessage());

                        }

                        binding.progressBar.setVisibility(View.GONE);
                        binding.loginButton.setEnabled(true);

                    }
                });

    }

    //for play animation
    private void setAnimation() {

        binding.loginAreaLayout.animate().alpha(0f).setDuration(1);
        TranslateAnimation animation = new TranslateAnimation(0, 0, 0, -1000);
        animation.setDuration(1500);
        animation.setFillAfter(false);
        animation.setAnimationListener(new MyAnimationListener());
        binding.textLayout.setAnimation(animation);

    }

    @Override
    protected void onStart() {
        super.onStart();

        if (FirebaseAuth.getInstance().getCurrentUser() != null) {

            startActivity(new Intent(this, HomeActivity.class));

        }

    }

    // For hide the status bar.
    private void makeFullScreen() {

        //getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        // Hide the app bar.
        if (getSupportActionBar() != null) {

            getSupportActionBar().hide();

        }

    }

    //class for animation listener
    private class MyAnimationListener implements Animation.AnimationListener {

        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {

            binding.textLayout.clearAnimation();
            binding.textLayout.setVisibility(View.INVISIBLE);
            binding.loginAreaLayout.animate().alpha(1f).setDuration(1500);

        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    }

    private void showToast(String message) {

        Toast.makeText(this, message, Toast.LENGTH_LONG).show();

    }

}