package com.ecom.park_jy;

import android.annotation.SuppressLint;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Setting extends AppCompatActivity {
    public static final String PARK = "PARK";
    public static final String PARK_USER = "PARK_USER";
    EditText edit_IP,edit_Port,edit6,account,edit8;
    Intent intent = new Intent();
    String ip = "192.168.3.2";
    int port = 21;
    public String user = "";
    public String FTP_user,FTP_paw,UserSave;
    private SharedPreferences share;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        edit_IP = (EditText)findViewById(R.id.editText4);
        edit_Port = (EditText)findViewById(R.id.editText5);
        edit6 = (EditText)findViewById(R.id.editText6);
        account = (EditText)findViewById(R.id.account);
        edit8 = (EditText)findViewById(R.id.editText8);

        edit_Port.setText(Integer.toString(port));
        edit_IP.setText(ip);
        if(user == null){
            user = getIntent().getStringExtra("USER_2");
        }else{
            user = getIntent().getStringExtra("USER");
        }
        savedata();
    }

    public void edit_selectUser(View v){
        intent.setClass(Setting.this,set_user_list.class);
        startActivity(intent);
        finish();
    }
    public void bt_back(View view){
        user = edit6.getText().toString();
        finish();
    }

    public void bt_save(View v){
        FTP_user = account.getText().toString();
        Toast.makeText(Setting.this,"設定保存成功",Toast.LENGTH_LONG).show();
    }

    private void savedata(){
        share = getSharedPreferences(PARK,0);
        edit6.setText(share.getString(PARK_USER, user));

        FTP_user = share.getString("PARK_ACCOUNT","cypark");
        account.setText(FTP_user);

        FTP_paw = share.getString("PARK_PASSWORD","ecom2828");
        edit8.setText(FTP_paw);
    }

    @Override
    protected void onPause() {
        super.onPause();
        share.edit().putString(PARK_USER,user).apply();
        share.edit().putString("PARK_ACCOUNT",FTP_user).apply();
        share.edit().putString("PARK_PASSWORD",FTP_paw).apply();
    }

    public boolean onKeyDown(int keyCode, KeyEvent event){
        if(keyCode == KeyEvent.KEYCODE_BACK){
            user = edit6.getText().toString();
            finish();
        }
        return true;
    }

}
