package chaoran.business.activity;

/*
 **********************************************
 *      DATE           PERSON       REASON
 *    2021-02-02          FXY        Created
 **********************************************
 */


import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import chaoran.business.R;

public class NetworkSettingActivity extends AppCompatActivity {

    private EditText address, path, port, screen_rotation, hide_bar;
    private Button save, cancel;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_network);
        initView();
    }


    private void initView() {
        setTitle(R.string.title_activity_setting_network);
        address = findViewById(R.id.address);
        path = findViewById(R.id.path);
        port = findViewById(R.id.port);
        screen_rotation = findViewById(R.id.screen_rotation);
        hide_bar = findViewById(R.id.hide_bar);
        save = findViewById(R.id.save);
        cancel = findViewById(R.id.cancel);
        SharedPreferences sharedPreferences = this.getSharedPreferences("crtech", Context.MODE_PRIVATE);
        address.setText(sharedPreferences.getString("address", ""));
        path.setText(sharedPreferences.getString("path", ""));
        port.setText(String.valueOf(sharedPreferences.getInt("port", -1)));
        screen_rotation.setText(String.valueOf(sharedPreferences.getInt("screen_rotation", 3)));
        hide_bar.setText(String.valueOf(sharedPreferences.getInt("hide_bar", 0)));
        cancel.setOnClickListener((e) -> this.finish());
        save.setOnClickListener((e) -> saveSetting());
    }

    private void saveSetting() {
        SharedPreferences.Editor editor = this.getSharedPreferences("crtech", Context.MODE_PRIVATE).edit();
        editor.putString("address", address.getText().toString().trim());
        editor.putString("path", path.getText().toString().trim());
        editor.putInt("port", Integer.parseInt(port.getText().toString().trim()));
        editor.putInt("screen_rotation", Integer.parseInt(screen_rotation.getText().toString().trim()));
        editor.putInt("hide_bar", Integer.parseInt(hide_bar.getText().toString().trim()));
        editor.commit();
        this.finish();
    }
}
