package com.cscodetech.movers.ui;


import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.Toast;

import com.cscodetech.movers.databinding.ActivityForgotPasswordBinding;
import com.cscodetech.movers.model.Contry;
import com.cscodetech.movers.model.RestResponse;
import com.cscodetech.movers.model.UserLogin;
import com.cscodetech.movers.retrofit.APIClient;
import com.cscodetech.movers.retrofit.GetResult;
import com.cscodetech.movers.utils.SessionManager;
import com.cscodetech.movers.utils.Utility;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;


public class ForgotPasswordActivity extends BaseActivity implements GetResult.MyListener {

    private ActivityForgotPasswordBinding binding;
    String applicationjson = "application/json";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityForgotPasswordBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        getCode();
        binding.btnContinus.setOnClickListener(v -> {
            if (android.text.TextUtils.isEmpty(binding.edUsername.getText().toString())) {
                binding.edUsername.setText("");
            } else {
                checkMobile();
            }
        });
        binding.lvlLogin.setOnClickListener(v -> finish());
    }


    private void checkMobile() {

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("mobile", binding.edUsername.getText().toString());
            jsonObject.put("ccode", binding.spinnerautocompet.getText().toString());

            RequestBody bodyRequest = RequestBody.create(jsonObject.toString(), MediaType.parse(applicationjson));
            Call<JsonObject> call = APIClient.getInterface().mobileCheck(bodyRequest);
            GetResult getResult = new GetResult(this);
            getResult.setMyListener(this);
            getResult.callForLogin(call, "2");

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void getCode() {
        JSONObject jsonObject = new JSONObject();
        RequestBody bodyRequest = RequestBody.create(jsonObject.toString(), MediaType.parse(applicationjson));
        Call<JsonObject> call = APIClient.getInterface().getCodelist(bodyRequest);
        GetResult getResult = new GetResult(this);
        getResult.setMyListener(this);
        getResult.callForLogin(call, "1");

    }

    @Override
    public void callback(JsonObject result, String callNo) {
        try {
            if (callNo.equalsIgnoreCase("1")) {
                Gson gson = new Gson();
                Contry contry = gson.fromJson(result.toString(), Contry.class);
                List<String> arealist = new ArrayList<>();
                for (int i = 0; i < contry.getCountryCode().size(); i++) {
                    if (contry.getCountryCode().get(i).getStatus().equalsIgnoreCase("1")) {
                        arealist.add(contry.getCountryCode().get(i).getCcode());
                    }
                }
                ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
                        android.R.layout.simple_dropdown_item_1line, arealist);
                binding.spinnerautocompet.setText(arealist.get(0));
                binding.spinnerautocompet.setAdapter(adapter);
            } else if (callNo.equalsIgnoreCase("2")) {
                Gson gson = new Gson();
                RestResponse response = gson.fromJson(result.toString(), RestResponse.class);
                if (response.getResult().equals("true")) {
                    Toast.makeText(this, "Number is not register", Toast.LENGTH_SHORT).show();

                } else {
                    if (response.getOtpAuth().equalsIgnoreCase("No")) {
                        Intent intent = new Intent(this, ChanegPasswordActivity.class);
                        intent.putExtra("phone", binding.edUsername.getText().toString());
                        startActivity(intent);
                    } else {
                        Utility.getInstance().isvarification = 0;
                        SessionManager sessionManager = new SessionManager(this);
                        UserLogin userLogin = new UserLogin();
                        userLogin.setMobile(binding.edUsername.getText().toString());
                        userLogin.setCcode(binding.spinnerautocompet.getText().toString());
                        sessionManager.setUserDetails(userLogin);
                        startActivity(new Intent(this, VerificationActivity.class));
                    }
                }
            }
        } catch (Exception e) {
            Log.e("Error", "-" + e.getMessage());

        }
    }
}