package w.soulofpluto.posstock;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.MenuItem;
import android.widget.EditText;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import static w.soulofpluto.posstock.StockReceived.startData;

 /*
 Create by Ramesh Babu
 */

public class StockTransferActivity extends AppCompatActivity {
    EditText tfs_number;
    Toolbar  StockTransfer_toolbar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_stock_transfer);
        super.onCreate(savedInstanceState);
        tfs_number            = findViewById(R.id.tfs_number);
        StockTransfer_toolbar = findViewById(R.id.stockTransfer_toolbar);
        setSupportActionBar(StockTransfer_toolbar);
        getSupportActionBar().setTitle(getResources().getString(R.string.stock_transfer));
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        tfs_number.addTextChangedListener(new Checked());
    }

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

    @Override
    protected void onRestart() {
        tfs_number.setText("");
        tfs_number.requestFocus();
        super.onRestart();
    }

    public class Checked implements TextWatcher {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }
        @Override
        public void onTextChanged(CharSequence val, int i, int i1, int i2) {
            try {
                if(!TextUtils.isEmpty(tfs_number.getText().toString())){
                    if (val.toString().length() > 0) {
                        Log.e("Perform ========>", val.toString());
                        if (val.toString().contains("\n")) {
                            if (Constants.isNetworkAvailable(StockTransferActivity.this)) {
                                startData(StockTransferActivity.this,val.toString().replace("\n",""));
                                tfs_number.setText("");
                                //tfs_number.requestFocus();
                                //return;
                            }
                        }
                    } else {
                        Constants.getToast(StockTransferActivity.this, "Enter TFSId No");
                        tfs_number.requestFocus();
                    }
                }
            } catch (NullPointerException jo){
                jo.getMessage();
            }
        }
        @Override
        public void afterTextChanged(Editable editable) {
        }
    }
}
