package com.app.simplitend.welcome.welcomecg.mvvm;

import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModel;

import com.app.simplitend.R;
import com.app.simplitend.welcome.welcomecg.WelcomeContracts;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Map;

import okhttp3.RequestBody;

public class CgWelcomeViewModel extends ViewModel {

    private static final String TAG = "CgWelcomeViewModel";

    private final CgWelcomeRepository repository;

    public CgWelcomeViewModel(){
        this.repository = CgWelcomeRepository.getRepository();
    }

    public void registerCareGiver(Map<String, RequestBody> body,
                                  @NonNull WelcomeContracts.RegisterCareGiverCallback registerCareGiverCallback){
        repository.registerCareGiver(body, registerCareGiverCallback);
    }

    public void verifyCgOTP(Map<String, String> body,
                            WelcomeContracts.VerifyCgOTPCallback verifyCgOTPCallback){
        repository.verifyCgOTP(body, verifyCgOTPCallback);
    }

    public void loginCaregiver(Map<String, String> body,
                               WelcomeContracts.CgLoginCallback loginCallback){
        repository.loginCaregiver(body, loginCallback);
    }

    public void sendForgotCodeEmail(String email,
                                    WelcomeContracts.ForgotPwdSentCodeCallback sentCodeCallback){
        repository.sendForgotCodeEmail(email, sentCodeCallback);
    }

    public void verifyForgotOTP(Map<String, String> body,
                                WelcomeContracts.VerifyForgotOTP forgotOTPCallback){
        repository.verifyForgotOTP(body, forgotOTPCallback);
    }

    public void changeCgPassword(Map<String, RequestBody> body,
                                 WelcomeContracts.ChangePasswordCallback changePasswordCallback){
        repository.changeCgPassword(body, changePasswordCallback);
    }

    public void connectCg(Map<String, String> body,
                          WelcomeContracts.ConnectCgCallback connectCgCallback){
        repository.connectCg(body, connectCgCallback);
    }

    public void resendCgCode(Map<String, String> body,
                             String token,
                             WelcomeContracts.ResendCgCodeCallback codeCallback){
        repository.resendCgCode(body, token, codeCallback);
    }

    public ArrayList<String> loadCountryCodeDropDown(Context context) {

        ArrayList<String> countryCodeList = new ArrayList<>();

        try {

            String countryCodeStr = readCountryCodes(context);
            JSONArray jsonArray = new JSONArray(countryCodeStr);

            for (int i = 0; i < jsonArray.length(); i++) {

                JSONObject code = jsonArray.getJSONObject(i);

                countryCodeList.add(code.getString("dial_code"));

            }

        } catch (Exception e) {

            // if cannot load all country codes showing only india's dial code
            countryCodeList.add("+91");
            countryCodeList.add("+1");

            Log.e(TAG, "loadCountryCodeDropDown: ", e);
        }

        return countryCodeList;
    }

    public String readCountryCodes(Context context) throws IOException {
        StringBuilder returnString = new StringBuilder();

        InputStream fIn = context.getResources().openRawResource(R.raw.country_code);
        InputStreamReader isr = new InputStreamReader(fIn);
        BufferedReader input = new BufferedReader(isr);

        String line = "";

        while ((line = input.readLine()) != null) {
            returnString.append(line);
        }

        try {
            isr.close();

            if (fIn != null) fIn.close();

            input.close();

        } catch (Exception e) {
            Log.e(TAG, "readCountryCodes: ", e);
        }

        return returnString.toString();
    }

}
