package com.yono.messeripos;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.textfield.TextInputEditText;
import com.yono.messeripos.api.service.UsersService;
import com.yono.messeripos.models.ForgotPasswordModels;
import com.yono.messeripos.models.UsersModels;
import com.yono.messeripos.response.DataResponse;
import com.yono.messeripos.utils.Utils;

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

public class ForgotPassword extends BottomSheetDialogFragment {

    BottomSheetDialog sheetDialog;
    BottomSheetBehavior sheetBehavior;
    View view;
    RelativeLayout resetPassUI;
    Utils utils = new Utils();
    TextInputEditText newPass, etConfPass;
    MaterialButton btnReset;
    Context context;
    UsersService usersService;
    ForgotPasswordModels forgotPasswordModels;

    public ForgotPassword(Context context, ForgotPasswordModels forgotPasswordModels) {
        this.context = context;
        this.forgotPasswordModels = forgotPasswordModels;
    }


    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        sheetDialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);

//        inflating layout
        view = View.inflate(getContext(), R.layout.forgot_new_password, null);

//        bindUI
        resetPassUI = view.findViewById(R.id.resetPassword);
        newPass = view.findViewById(R.id.etNewPassword);
        etConfPass = view.findViewById(R.id.etPasswordConfirmation);
        btnReset = view.findViewById(R.id.btnReset);


        sheetDialog.setContentView(view);

        sheetBehavior = BottomSheetBehavior.from((View) (view.getParent()));

//        get Window size
        int setHeight = utils.getWindowHeight() - (utils.getWindowHeight() * 5) / 100;

//        set max height
        sheetBehavior.setPeekHeight(setHeight);
        resetPassUI.setMinimumHeight(setHeight);

        btnReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String newPas = newPass.getText().toString();
                String conPass = etConfPass.getText().toString();

                forgotPasswordModels.setPassword(newPass.getText().toString());

                if (newPas.equals(conPass)) {
                    sheetDialog.dismiss();
                    usersService.resetPassword(forgotPasswordModels);

                    startActivity(new Intent(context, LoginActivity.class));
                    getActivity().finish();
                }else {
                    utils.dialog(context, true, "password does't match", "Your password not match!").show();
                }
            }
        });

        return sheetDialog;
    }
}
