package ch.spherIC.recurvebowsight.ui.configuration;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;

import java.text.DecimalFormat;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import ch.spherIC.recurvebowsight.R;
import ch.spherIC.recurvebowsight.databinding.ActivityDefaultSettingsBinding;
import ch.spherIC.recurvebowsight.utilities.PlatformUtils;

public class DefaultSettingsActivity extends AppCompatActivity {

    public static final DecimalFormat DF = new DecimalFormat("0.#");

    private ActivityDefaultSettingsBinding binding;
    private EditText defaultAirTemp;
    private EditText defaultRelAirHumidity;
    private EditText defaultShootingDistance;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_default_settings);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle(R.string.caption_Cfg_DefaultValues_Title);

        this.binding = ActivityDefaultSettingsBinding.bind(findViewById(R.id.activity_default_settings));

        this.binding.defauButtonSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveDefaultValues();
            }
        });
        loadDefaultValues();
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void loadDefaultValues() {
        SharedPreferences sharedPreferences = getSharedPreferences(PlatformUtils.DEFAULT_SETTINGS_PREF, MODE_PRIVATE);
        if (sharedPreferences != null) {
            this.binding.airTemperatureTxt.setText(getFloatStingVal(sharedPreferences.getFloat(PlatformUtils.DEFAULT_SETTING_AIRTEMP, 0.0f)));
            this.binding.airRelativeHumidityTxt.setText(getFloatStingVal(sharedPreferences.getFloat(PlatformUtils.DEFAULT_SETTING_RELAIRHUMIDITY, 0.0f)));
            this.binding.shootingDistanzTxt.setText(getFloatStingVal(sharedPreferences.getFloat(PlatformUtils.DEFAULT_SETTING_SHOOTINGDIST, 0.0f)));
        }
    }

    private void saveDefaultValues() {
        SharedPreferences sharedPreferences = getSharedPreferences(PlatformUtils.DEFAULT_SETTINGS_PREF, MODE_PRIVATE);
        if (sharedPreferences != null) {
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putFloat(PlatformUtils.DEFAULT_SETTING_AIRTEMP, Float.parseFloat(this.binding.airTemperatureTxt.getText().toString()));
            editor.putFloat(PlatformUtils.DEFAULT_SETTING_RELAIRHUMIDITY, Float.parseFloat(this.binding.airRelativeHumidityTxt.getText().toString()));
            editor.putFloat(PlatformUtils.DEFAULT_SETTING_SHOOTINGDIST, Float.parseFloat(this.binding.shootingDistanzTxt.getText().toString()));
            editor.commit();
        }
    }

    private String getFloatStingVal(float val) {
        return val == 0.0 ? "" : DF.format(val);
    }


}
