package com.example.jinyu_rfid;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;

import com.example.jinyu_rfid.databinding.ActivityAppLoginBinding;
import com.example.jinyu_rfid.uitls.SharedPreferencesUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Locale;

public class AppLoginActivity extends AppCompatActivity {
    private ActivityAppLoginBinding binding;

    private Resources resources;
    private Configuration config;
    private int index;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        resources = getResources();
        config = resources.getConfiguration();
        var languageString = SharedPreferencesUtils.getstring("language", "中文");
        getLocaleStringXML(languageString);
        resources.updateConfiguration(config, resources.getDisplayMetrics());
        binding = DataBindingUtil.setContentView(this, R.layout.activity_app_login);
        binding.appLanguage.setSelection(index);
        var workmode = SharedPreferencesUtils.getInt("workmode", 0);
        binding.mainWork.setSelection(workmode);
    }

    public void appLoginClick(View view) {
        // 工作模式 0=net
        var workModeStr = binding.mainWork.getSelectedItem().toString();
        var strings = Arrays.asList(getResources().getStringArray(R.array.work_mode));
        SharedPreferencesUtils.putInt("workmode", strings.indexOf(workModeStr));
        // 选择语言
        var s = binding.appLanguage.getSelectedItem().toString();
        SharedPreferencesUtils.putstring("language", s);
        getLocaleStringXML(s);
        resources.updateConfiguration(config, resources.getDisplayMetrics());


        startActivity(new Intent(this, ReadActivity.class));
        finish();
    }

    private void getLocaleStringXML(String languageString) {
        if (languageString.equals("中文")) {
            config.locale = Locale.CHINA;
            index = 0;
        } else if (languageString.equals("English")) {
            config.locale = Locale.ENGLISH;
            index = 1;
        } else {
            config.locale = new Locale("vi", "VN");
            index = 2;
        }
    }
}