package com.dspread.pos.ui.setting.device_config;

import static com.dspread.pos.ui.setting.device_config.DeviceConfigActivity.TYPE_LANGUAGE;

import android.app.Application;
import android.icu.util.Currency;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import androidx.lifecycle.MutableLiveData;


import com.dspread.pos.common.enums.PaymentType;
import com.dspread.pos.common.enums.TransCardMode;
import com.dspread.pos_android_app.R;
import com.mynameismidori.currencypicker.ExtendedCurrency;

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

import me.goldze.mvvmhabit.base.BaseViewModel;

public class DeviceConfigSelectionViewModel extends BaseViewModel {
    public ObservableField<String> searchText = new ObservableField<>("");
    public MutableLiveData<List<DeviceConfigItem>> currencyList = new MutableLiveData<>();
    private List<DeviceConfigItem> allCurrencies = new ArrayList<>();

    // Add language list initialization
    private void initLanguageList() {
        // Supported languages (you can expand this list)
        String[] languages = {
                "English (en)",
                "Russian (ru)",
                "Uzbek (uz)",
                "Kazakh (kk)",
                "Arabic (ar)",
                "Chinese (zh)"
        };

//        String currentLang = SPUtils.getInstance().getString("app_language", "en");
        String currentLang = "en";

        for (String language : languages) {
            String[] parts = language.split("\\(");
            String name = parts[0].trim();
            String code = parts[1].replace(")", "").trim();

            allCurrencies.add(new DeviceConfigItem(
                    code,
                    name,
                    getFlagForLanguage(code),  // You'll need to implement this
                    currentLang.equals(code) ? 1 : 0  // Using numericCode to track selection
            ));
        }
        currencyList.setValue(allCurrencies);
    }
    // Helper method to get flags for languages
    private int getFlagForLanguage(String languageCode) {
        switch (languageCode) {
            case "en": return 2131230940;  // English
            case "ru": return 2131230927;
            case "uz": return 2131230927;
            case "kk": return 2131230897;
            case "ar": return 2131230853;
            case "zh": return 2131230870;
            default: return R.drawable.ic_transaction_type;
        }
    }


    public void init(int type) {
        allCurrencies.clear(); // Clear old data
        switch (type) {
            case DeviceConfigActivity.TYPE_CURRENCY:
                initCurrencyList();
                break;
            case DeviceConfigActivity.TYPE_TRANSACTION:
                initTransactionList();
                break;
            case DeviceConfigActivity.TYPE_CARD_MODE:
                initCardModeList();
                break;
            case TYPE_LANGUAGE:
                initLanguageList();
                break;
        }
    }
    
    public DeviceConfigSelectionViewModel(@NonNull Application application) {
        super(application);
    }

    private void initTransactionList() {
        String[] types = PaymentType.getValues();
        for (int i = 0 ; i < types.length; i++) {
            String type = types[i];
            allCurrencies.add(new DeviceConfigItem(
                    type,
                    type,
                    R.drawable.ic_transaction_type,
                    0
            ));
        }
        currencyList.setValue(allCurrencies);
    }

    private void initCurrencyList() {
        List<ExtendedCurrency> currencies = ExtendedCurrency.getAllCurrencies();
        for (ExtendedCurrency currency : currencies) {
            try {
                Currency currencyInstance = Currency.getInstance(currency.getCode());
                int numericCode = currencyInstance.getNumericCode();

                // Skip Ukrainian Hryvnia (UAH, numeric code: 980)
                if (numericCode == 980) {
                    continue; // Skip this currency
                }

                allCurrencies.add(new DeviceConfigItem(
                        currency.getCode(),
                        currency.getName(),
                        currency.getFlag(),  // Use the flag resource ID provided by the library directly here
                        numericCode
                ));
            }catch (Exception e){
                continue;
            }
        }
        currencyList.setValue(allCurrencies);
    }

    private void initCardModeList() {
        String[] modes = TransCardMode.getCardTradeModes();
        for (int i = 0 ; i < modes.length; i++) {
            String mode = modes[i];
            allCurrencies.add(new DeviceConfigItem(
                    "",
                    mode,
                    R.drawable.ic_transaction_type,
                    0
            ));
        }
        currencyList.setValue(allCurrencies);
    }

    public void filterCurrencies(String query) {
        if (query.isEmpty()) {
            currencyList.setValue(allCurrencies);
        } else {
            List<DeviceConfigItem> filteredList = new ArrayList<>();
            for (DeviceConfigItem currency : allCurrencies) {
                if (currency.getName().toLowerCase().contains(query.toLowerCase()) ||
                        currency.getCode().toLowerCase().contains(query.toLowerCase())) {
                    filteredList.add(currency);
                }
            }
            currencyList.setValue(filteredList);
        }
    }
}