package com.cscodetech.moverslorry.ui;


import static com.cscodetech.moverslorry.utils.SessionManager.intro;
import static com.cscodetech.moverslorry.utils.SessionManager.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Toast;

import com.cscodetech.moverslorry.R;
import com.cscodetech.moverslorry.databinding.ActivityLogBinding;
import com.cscodetech.moverslorry.model.Contry;
import com.cscodetech.moverslorry.model.Login;
import com.cscodetech.moverslorry.model.RestResponse;
import com.cscodetech.moverslorry.retrofit.APIClient;
import com.cscodetech.moverslorry.retrofit.GetResult;
import com.cscodetech.moverslorry.utils.CustPrograssbar;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.onesignal.OneSignal;

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 LoginActivity extends BaseActivity implements GetResult.MyListener {

    private ActivityLogBinding binding;
    CustPrograssbar custPrograssbar;
    String applicationjson="application/json";
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityLogBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        custPrograssbar = new CustPrograssbar();
        getCode();

        // Wire clicks via ViewBinding
        binding.txtForgot.setOnClickListener(this::onBindClick);
        binding.btnLogin.setOnClickListener(this::onBindClick);
        binding.lvlSignup.setOnClickListener(this::onBindClick);

    }

    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");

    }

    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");
            custPrograssbar.prograssCreate(LoginActivity.this);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

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

            RequestBody bodyRequest = RequestBody.create(jsonObject.toString(),MediaType.parse(applicationjson));
            Call<JsonObject> call = APIClient.getInterface().userLogin(bodyRequest);
            GetResult getResult = new GetResult(this);
            getResult.setMyListener(this);
            getResult.callForLogin(call, "3");
            custPrograssbar.prograssCreate(LoginActivity.this);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void onBindClick(View view) {
        int id = view.getId();
        if (id == R.id.txt_forgot) {
            startActivity(new Intent(LoginActivity.this, ForgotPasswordActivity.class));
        } else if (id == R.id.btn_login) {
            if (binding.lvlPassword.getVisibility() == View.VISIBLE) {
                if (!TextUtils.isEmpty(binding.edUsername.getText().toString()) && !TextUtils.isEmpty(binding.editPassword.getText().toString())) {
                    login();
                }

            } else {
                if (!TextUtils.isEmpty(binding.edUsername.getText().toString())) {
                    checkMobile();
                }
            }
        } else if (id == R.id.lvl_signup) {
            startActivity(new Intent(LoginActivity.this, SignUpActivity.class));
        }
    }

    @Override
    public void callback(JsonObject result, String callNo) {
        try {
            custPrograssbar.closePrograssBar();
            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 {

                    binding.lvlPassword.setVisibility(View.VISIBLE);

                }
            } else if (callNo.equalsIgnoreCase("3")) {
                Gson gson = new Gson();
                Login loginUser = gson.fromJson(result.toString(), Login.class);
                Toast.makeText(this, loginUser.getResponseMsg(), Toast.LENGTH_LONG).show();
                if (loginUser.getResult().equalsIgnoreCase("true")) {

                    sessionmanager.setUserDetails(loginUser.getUserLogin());

                    sessionmanager.setBooleanData(login, true);
                    sessionmanager.setBooleanData(intro, true);

                    OneSignal.sendTag("owner_id", loginUser.getUserLogin().getId());

                    startActivity(new Intent(this, HomeActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK));
                }
            }
        } catch (Exception e) {
            Log.e("Error","--> "+e.getMessage());

        }
    }
}