package com.ssiltda.printerapp;

import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class configVend extends AppCompatActivity {

    EditText txtNomImpre, txtNitComp, txtNomComp, txtDirComp, txtTelComp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_conf_vend);
        Button btncont = (Button) findViewById(R.id.cont);
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        txtNomImpre = (EditText) findViewById(R.id.nomImpre);
        txtNitComp = (EditText) findViewById(R.id.nitcomp);
        txtNomComp = (EditText) findViewById(R.id.nomcomp);
        txtDirComp = (EditText) findViewById(R.id.dircomp);
        txtTelComp = (EditText) findViewById(R.id.telcomp);
        txtNomImpre.setText(preferences.getString("nombImpresora", "").toString());
        txtNitComp.setText(preferences.getString("nitComp", "").toString());
        txtNomComp.setText(preferences.getString("nombComp", "").toString());
        txtDirComp.setText(preferences.getString("dirComp", "").toString());
        txtTelComp.setText(preferences.getString("telComp", "").toString());
        btncont.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(txtNomImpre.getText().toString().equals("")){
                    Toast.makeText(configVend.this, "Debe ingresar el nombre de la impresora", Toast.LENGTH_SHORT).show();
                }else if(txtNitComp.getText().toString().equals("")){
                    Toast.makeText(configVend.this, "Debe ingresar el nit de la compañia", Toast.LENGTH_SHORT).show();
                }else if(txtNomComp.getText().toString().equals("")){
                    Toast.makeText(configVend.this, "Debe ingresar el nombre de la compañia", Toast.LENGTH_SHORT).show();
                }else{
                    saveVendCod();
                }
            }
        });
    }

    private void saveVendCod(){
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("nombImpresora", txtNomImpre.getText().toString());
        editor.putString("nitComp", txtNitComp.getText().toString());
        editor.putString("nombComp", txtNomComp.getText().toString());
        editor.putString("dirComp", txtDirComp.getText().toString());
        editor.putString("telComp", txtTelComp.getText().toString());
        editor.apply();
        intentini();
    }

    public void intentini(){
        startActivity(new Intent(getBaseContext(), Inicio.class)
                .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP));
        finish();
    }
}