package com.cscodetech.movers.ui;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.cscodetech.movers.databinding.ActivityChanegPasswordBinding;
import com.cscodetech.movers.model.RestResponse;
import com.cscodetech.movers.retrofit.APIClient;
import com.cscodetech.movers.retrofit.GetResult;
import com.cscodetech.movers.utils.CustPrograssbar;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;

public class ChanegPasswordActivity extends BaseActivity implements GetResult.MyListener {

    private ActivityChanegPasswordBinding binding;
    String phoneNumber;
    CustPrograssbar custPrograssbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityChanegPasswordBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        phoneNumber = getIntent().getStringExtra("phone");
        custPrograssbar = new CustPrograssbar();
        binding.btnSubmit.setOnClickListener(v -> {
            if (validation())
                setPassword();
        });
    }

    public boolean validation() {
        if (binding.edPassword.getText().toString().isEmpty()) {
            binding.edPassword.setError("Enter Password");
            return false;
        }
        if (binding.edConpassword1.getText().toString().isEmpty()) {
            binding.edConpassword1.setError("Enter Confirm");
            return false;
        }
        if (!binding.edConpassword1.getText().toString().equals(binding.edPassword.getText().toString())) {
            binding.edConpassword1.setError("Mismatch Password");
            binding.edPassword.setError("Mismatch Password");
            return false;
        }
        return true;
    }



    private void setPassword() {
        custPrograssbar.prograssCreate(ChanegPasswordActivity.this);
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("mobile", phoneNumber);
            jsonObject.put("password", binding.edPassword.getText().toString());
            RequestBody bodyRequest = RequestBody.create(jsonObject.toString(),MediaType.parse("application/json"));
            Call<JsonObject> call = APIClient.getInterface().getForgot(bodyRequest);
            GetResult getResult = new GetResult(this);
            getResult.setMyListener(this);
            getResult.callForLogin(call, "1");


        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void callback(JsonObject result, String callNo) {
        try {
            custPrograssbar.closePrograssBar();
            Gson gson = new Gson();
            RestResponse response = gson.fromJson(result.toString(), RestResponse.class);
            Toast.makeText(ChanegPasswordActivity.this, "" + response.getResponseMsg(), Toast.LENGTH_LONG).show();
            if (response.getResult().equals("true")) {
                Intent intent = new Intent(ChanegPasswordActivity.this, LoginActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent);
                finish();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
