package in.techware.carrefour.activity.it;

import android.arch.lifecycle.ViewModelProviders;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.HapticFeedbackConstants;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import java.util.ArrayList;

import in.techware.carrefour.R;
import in.techware.carrefour.activity.BaseAppCompatNoDrawerActivity;
import in.techware.carrefour.activity.FileTransferActivity;
import in.techware.carrefour.config.Config;
import in.techware.carrefour.dbUtils.repositories.InternalSalesCorrectionRepository;
import in.techware.carrefour.dialogs.ConfirmationDialog;
import in.techware.carrefour.dialogs.SelectDialog;
import in.techware.carrefour.listeners.BarcodeUpdateListener;
import in.techware.carrefour.model.BasicDataBean;
import in.techware.carrefour.model.InternalSalesCorrectionBean;
import in.techware.carrefour.model.MenuBean;
import in.techware.carrefour.model.SectionBean;
import in.techware.carrefour.util.AppConstants;
import in.techware.carrefour.util.BarcodeUtil;
import in.techware.carrefour.util.menuUtils.DropDownMenuUtil;
import in.techware.carrefour.viewModels.InternalSalesCorrectionViewModel;

public class InternalSalesCorrectionActivity extends BaseAppCompatNoDrawerActivity {


    private static final String TAG = "InternalSalesCA";
    private CheckBox cbQuantity;
    private EditText etxtQuantity;
    private EditText etxtDepartment;
    private EditText etxtSection;
    private EditText etxtBarcode;
    private Button btnDepartmentChange;
    private Button btnSectionChange;
    private Button btnBack;
    private Button btnModify;
    private Button btnDelete;
    private MenuBean selectedMenuBean;
    private MenuBean selectedSubMenuBean;
    private InternalSalesCorrectionViewModel viewModel;
    private TextView txtLastEntryBarcode;
    private TextView txtLastEntryQuantity;
    private LinearLayout llDepartment;
    private LinearLayout llSection;
    private TextView txtDepartment;
    private TextView txtSection;
    private InternalSalesCorrectionRepository internalSalesCorrectionRepository;
    private InternalSalesCorrectionRepository.InternalSalesCorrectionRepositoryListener internalSalesCorrectionRepositoryListener;
    private View.OnClickListener clickListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_internal_sales_correction);

        if (getIntent().hasExtra("menuBean")) {
            selectedMenuBean = (MenuBean) getIntent().getSerializableExtra("menuBean");
        }
        if (getIntent().hasExtra("subMenuBean")) {
            selectedSubMenuBean = (MenuBean) getIntent().getSerializableExtra("subMenuBean");
        }

        viewModel = (InternalSalesCorrectionViewModel) ViewModelProviders.of(this).get(InternalSalesCorrectionViewModel.class);
        viewModel.setSelectedMenuBean(selectedMenuBean);
        viewModel.setSelectedSubMenuBean(selectedSubMenuBean);

        initViews();
        handleKeyboard();

        populateViewModel();

        if (getSupportActionBar() != null) {
            setTitle(R.string.title_sec_transfer, R.color.white);
            getSupportActionBar().setTitle("");
            getSupportActionBar().setHomeButtonEnabled(true);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowTitleEnabled(true);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == REQ_FILE_TRANSFER && resultCode == RESULT_OK) {
            if (data != null && data.hasExtra("result")) {
                if (data.getIntExtra("result", 0) == AppConstants.FILE_TRANSFER_RESULT_SEND_DATA_COMPLETED) {
                    finish();
                }
            }
        }
    }


    @Override
    public void onResume() {
        super.onResume();

        if (viewModel.getInternalSalesCorrectionList().isEmpty()) {
            setProgressScreenVisibility(true, true);
            getData(false);
            viewModel.setDepartmentList(DropDownMenuUtil.getDepartmentList());
        } else {
            getData(true);
        }

    }

    private void getData(boolean isSwipeRefreshing) {
        swipeView.setRefreshing(isSwipeRefreshing);
        InternalSalesCorrectionRepository.getInstance().getAllUnsentInternalSalesCorrection(internalSalesCorrectionRepositoryListener);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_base, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case android.R.id.home:
                toolbar.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
                //                mVibrator.vibrate(25);
                onInternalSalesCorrectionBackClick(toolbar);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void initViews() {

        llDepartment = findViewById(R.id.ll_internal_sales_correction_department);
        txtDepartment = findViewById(R.id.txt_internal_sales_correction_department);
        llSection = findViewById(R.id.ll_internal_sales_correction_section);
        txtSection = findViewById(R.id.txt_internal_sales_correction_section);
        cbQuantity = findViewById(R.id.cb_internal_sales_correction_quantity);
        etxtQuantity = findViewById(R.id.etxt_internal_sales_correction_quantity);
        etxtDepartment = findViewById(R.id.etxt_internal_sales_correction_department);
        etxtSection = findViewById(R.id.etxt_internal_sales_correction_section);
        etxtBarcode = findViewById(R.id.etxt_internal_sales_correction_barcode);

        txtLastEntryBarcode = findViewById(R.id.txt_internal_sales_correction_last_entry_barcode);
        txtLastEntryQuantity = findViewById(R.id.txt_internal_sales_correction_last_entry_quantity);

        btnDepartmentChange = findViewById(R.id.btn_internal_sales_correction_department_change);
        btnSectionChange = findViewById(R.id.btn_internal_sales_correction_section_change);
        btnBack = findViewById(R.id.btn_internal_sales_correction_back);
        btnModify = findViewById(R.id.btn_internal_sales_correction_modify);
        btnDelete = findViewById(R.id.btn_internal_sales_correction_delete);

        llDepartment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onInternalSalesCorrectionDepartmentClick(v);
            }
        });
        llSection.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onInternalSalesCorrectionSectionClick(v);
            }
        });
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onInternalSalesCorrectionBackClick(v);
            }
        });
        btnDepartmentChange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onInternalSalesCorrectionDepartmentChangeClick(v);
            }
        });
        btnSectionChange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onInternalSalesCorrectionSectionChangeClick(v);
            }
        });
        btnModify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onInternalSalesCorrectionModifyClick(v);
            }
        });
        btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onInternalSalesCorrectionDeleteClick(v);
            }
        });


        internalSalesCorrectionRepository = InternalSalesCorrectionRepository.getInstance();

        internalSalesCorrectionRepositoryListener = new InternalSalesCorrectionRepository.InternalSalesCorrectionRepositoryListener() {
            @Override
            public void onAddCompleted(InternalSalesCorrectionBean internalSalesCorrectionBean) {
                viewModel.getInternalSalesCorrectionList().add(internalSalesCorrectionBean);
                viewModel.clearInternalSalesCorrection();
                viewModel.setLastEntry(internalSalesCorrectionBean);
                viewModel.setModifyEnabled(false);
                populateViewModel();
            }

            @Override
            public void onDeleteCompleted(InternalSalesCorrectionBean internalSalesCorrectionBean) {
                int index = viewModel.indexOf(internalSalesCorrectionBean);
                if (index != -1)
                    viewModel.getInternalSalesCorrectionList().remove(index);
                populateViewModel();
            }

            @Override
            public void onDeleteAllCompleted() {

            }

            @Override
            public void onDeleteAllSentCompleted() {

            }

            @Override
            public void onDeleteAllUnsentCompleted() {

            }

            @Override
            public void onUpdateCompleted(InternalSalesCorrectionBean internalSalesCorrectionBean) {
                int index = viewModel.indexOf(internalSalesCorrectionBean);
                if (index != -1) {
                    viewModel.getInternalSalesCorrectionList().set(index, internalSalesCorrectionBean);
                    viewModel.clearInternalSalesCorrection();
                    viewModel.setLastEntry(viewModel.getInternalSalesCorrectionList().get(viewModel.lastIndex()));
                    viewModel.setModifyEnabled(false);
                    populateViewModel();
                    etxtBarcode.requestFocus();
                }
            }

            @Override
            public void onInternalSalesCorrectionBeanLoaded(InternalSalesCorrectionBean internalSalesCorrectionBean) {

            }

            @Override
            public void onAllListLoaded(ArrayList<InternalSalesCorrectionBean> internalSalesCorrectionListBean) {

            }

            @Override
            public void onAllUnsentListLoaded(ArrayList<InternalSalesCorrectionBean> internalSalesCorrectionList) {
                viewModel.setInternalSalesCorrectionList(internalSalesCorrectionList);
                populateViewModel();
                swipeView.setRefreshing(false);
                setProgressScreenVisibility(false, false);
            }

            @Override
            public void onAllSentListLoaded(ArrayList<InternalSalesCorrectionBean> internalSalesCorrectionListBean) {

            }

            @Override
            public void onFailed() {

            }
        };

        etxtQuantity.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                Log.i(TAG, "onFocusChange: QUANTITY FOCUS : " + hasFocus);
                if (hasFocus) {
                    etxtQuantity.setText("");
                } /*else {
                    etxtQuantity.setText("1");
                }*/
            }
        });
        etxtQuantity.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (etxtQuantity.getText().toString().length() > 0) {
                    float value = Float.parseFloat(etxtQuantity.getText().toString());
                    if (value > 99999.999) {
                        etxtQuantity.setText(R.string.sample_max_quantity);
                    } else if (value < 1) {
                        etxtQuantity.setText("1");
                    }

                    Log.i(TAG, "afterTextChanged: Formatted Quantity : " + BarcodeUtil.getFormattedQuantity(value, 8, true));
                }
            }

        });
        etxtQuantity.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if ((event.getAction() == KeyEvent.ACTION_UP) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                    Log.i(TAG, "onEditorAction: " + etxtQuantity.getText().toString());
                    if (etxtQuantity.getText().toString().length() > 0) {
                        final float quantity = Float.parseFloat(etxtQuantity.getText().toString());
                        if (quantity > 10) {
                            ConfirmationDialog confirmationDialog = new ConfirmationDialog(InternalSalesCorrectionActivity.this);
                            confirmationDialog.setConfirmationDialogActionListener(
                                    new ConfirmationDialog.ConfirmationDialogActionListener() {
                                        @Override
                                        public void onYesClicked() {
                                            viewModel.setQuantity(quantity);
                                            //                                            viewModel.setQuantityEnabled(false);
                                            if (viewModel.isDataCollected()) {
                                                saveInternalSalesCorrectionBean();
                                            }
                                        }

                                        @Override
                                        public void onNoClicked() {
                                            viewModel.setQuantity(1);
                                            //                                            viewModel.setQuantityEnabled(false);
                                            if (viewModel.isDataCollected()) {
                                                saveInternalSalesCorrectionBean();
                                            }
                                        }
                                    });
                            confirmationDialog.show(R.string.message_quantity_is_greater_than_10);
                        } else {
                            viewModel.setQuantity(quantity);
                            //                            viewModel.setQuantityEnabled(false);
                            if (viewModel.isDataCollected()) {
                                saveInternalSalesCorrectionBean();
                            }
                        }
                    }
                    return true; // consume.
                }
                return false; // pass on to other listeners.
            }
        });
        etxtBarcode.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if ((event.getAction() == KeyEvent.ACTION_UP) && (keyCode == KeyEvent.KEYCODE_ENTER)) {

                    String barcode = etxtBarcode.getText().toString();
                    etxtBarcode.setText(BarcodeUtil.getFormattedBarcode(barcode));
                    viewModel.setBarcode(barcode);

                    if (viewModel.isDataCollected() && !viewModel.isQuantityEnabled()) {
                        saveInternalSalesCorrectionBean();
                    } else if (viewModel.isQuantityEnabled()) {
                        etxtQuantity.requestFocus();
                    }

                    return true; // consume.

                }
                return false; // pass on to other listeners.
            }
        });

        cbQuantity.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Log.i(TAG, "onCheckedChanged: CHECKED STATUS : " + isChecked);
                etxtQuantity.setEnabled(isChecked);
                etxtQuantity.setTextIsSelectable(isChecked);
                etxtQuantity.setFocusable(isChecked);
                etxtQuantity.setFocusableInTouchMode(isChecked);

                viewModel.setQuantity(1);
                viewModel.setQuantityEnabled(isChecked);
                etxtQuantity.setText(!isChecked ? "" : BarcodeUtil.removeTrailingZeroes(viewModel.getQuantity()));
                if (isChecked) {
                    etxtQuantity.requestFocus();
                } else {
                    etxtQuantity.clearFocus();
                }
//                populateViewModel();
            }
        });

        BarcodeUpdateListener barcodeUpdateListener = new BarcodeUpdateListener() {
            @Override
            public void onBarcodeUpdated(String barcode) {
                etxtBarcode.setText(barcode);
                viewModel.setBarcode(barcode);
                if (viewModel.isDataCollected() && !viewModel.isQuantityEnabled()) {
                    saveInternalSalesCorrectionBean();
                } else if (viewModel.isQuantityEnabled()) {
                    etxtQuantity.requestFocus();
                }
            }
        };
        addBarcodeUpdateListener(barcodeUpdateListener);
    }

    private void onSaveCompleted() {
        viewModel.clearInternalSalesCorrection();
        viewModel.setLastEntry(viewModel.getInternalSalesCorrectionList().get(viewModel.lastIndex()));
        viewModel.setModifyEnabled(false);
        populateViewModel();
        etxtBarcode.requestFocus();
    }

    private void handleKeyboard() {
        if (getSharedPreferences(AppConstants.PREF_FILE, 0).getBoolean(AppConstants.KEYBOARD_STATUS, false)) {
            etxtBarcode.setFocusableInTouchMode(true);
        } else {
//            etxtZoneType.setOnFocusChangeListener(focusChangedListener);
//            etxtBarcode.setOnFocusChangeListener(focusChangedListener);
//            etxtQuantity.setOnFocusChangeListener(focusChangedListener);
            etxtBarcode.setFocusableInTouchMode(false);
            setClickListener(etxtBarcode);
        }
    }

    private void setClickListener(final EditText etxt) {
        clickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                etxt.setEnabled(true);
                etxt.setTextIsSelectable(true);
                etxt.setFocusable(true);
                etxt.setFocusableInTouchMode(true);

                if (etxt.isEnabled() && !etxt.hasFocus()) {
                    etxt.requestFocus();
                }

                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
            }
        };
    }


    private void populateViewModel() {

        Log.i(TAG, "populateViewModel: VIEW MODEL : " + new Gson().toJson(viewModel));

        etxtDepartment.setText(viewModel.getDepartmentCode());
        etxtSection.setText(viewModel.getSectionCode());
        etxtBarcode.setText(!viewModel.isModifyEnabled() ? viewModel.getBarcode() : BarcodeUtil.getFormattedBarcode(viewModel.getBarcode()));
        etxtQuantity.setText(!viewModel.isModifyEnabled() ? "" : BarcodeUtil.removeTrailingZeroes(viewModel.getQuantity()));
//        etxtQuantity.setText(String.valueOf(viewModel.getQuantity()));
        cbQuantity.setChecked(viewModel.isQuantityEnabled());

        if (viewModel.getDepartmentSelectedPosition() != -1) {
            txtDepartment.setText(viewModel.getDepartment());
            llSection.setEnabled(true);
            if (viewModel.getSectionSelectedPosition() != -1) {
                txtSection.setText(viewModel.getSection());
                etxtQuantity.setFocusable(viewModel.isQuantityEnabled());
                etxtQuantity.setEnabled(viewModel.isQuantityEnabled());
                enableViews(true);
            } else {
                enableViews(false);
                llSection.setEnabled(true);
            }
        } else {
            enableViews(false);
            llSection.setEnabled(false);
        }

        if (!viewModel.getInternalSalesCorrectionList().isEmpty()) {
            viewModel.setLastEntry(viewModel.getInternalSalesCorrectionList().get(viewModel.lastIndex()));
        } else {
            viewModel.setLastEntry(null);
        }
        if (viewModel.getLastEntry() != null) {
            txtLastEntryBarcode.setText(viewModel.getLastEntry().getBarcode());
            txtLastEntryQuantity.setText(BarcodeUtil.removeTrailingZeroes(viewModel.getLastEntry().getQuantity()));
        } else {
            txtLastEntryBarcode.setText("");
            txtLastEntryQuantity.setText("");
        }

        if (viewModel.getInternalSalesCorrectionList().size() > 0) {
            btnDelete.setEnabled(true);
            btnModify.setEnabled(true);
        } else {
            btnDelete.setEnabled(false);
            btnModify.setEnabled(false);
        }


    }

    private void saveInternalSalesCorrectionBean() {
        InternalSalesCorrectionBean internalSalesCorrectionBean = viewModel.generateInternalSalesCorrectionBean();

        if (viewModel.isModifyEnabled()) {
            internalSalesCorrectionBean.setId(viewModel.getLastEntry().getId());
            internalSalesCorrectionRepository.updateInternalSalesCorrection(internalSalesCorrectionBean, internalSalesCorrectionRepositoryListener);
        } else {
            internalSalesCorrectionRepository.insertInternalSalesCorrection(internalSalesCorrectionBean, internalSalesCorrectionRepositoryListener);
        }
    }

    private void enableViews(boolean isEnable) {

        llDepartment.setEnabled(!isEnable);
        llSection.setEnabled(!isEnable);
        Log.i(TAG, "enableViews: etxtQuantity : " + (viewModel.isQuantityEnabled() && isEnable));
        etxtQuantity.setEnabled(viewModel.isQuantityEnabled() && isEnable);
        etxtBarcode.setEnabled(isEnable);
        cbQuantity.setEnabled(isEnable);

        llDepartment.setFocusable(!isEnable);
        llSection.setFocusable(!isEnable);
        etxtQuantity.setFocusable(viewModel.isQuantityEnabled() && isEnable);
        etxtBarcode.setFocusable(isEnable);
        cbQuantity.setFocusable(isEnable);

        etxtBarcode.setTextIsSelectable(isEnable);
        etxtBarcode.setFocusableInTouchMode(isEnable);
        etxtQuantity.setTextIsSelectable(viewModel.isQuantityEnabled() && isEnable);
        etxtQuantity.setFocusableInTouchMode(viewModel.isQuantityEnabled() && isEnable);

        if (isEnable && !etxtBarcode.hasFocus()) {
            etxtBarcode.requestFocus();
        }
    }

    public void onInternalSalesCorrectionDepartmentClick(View view) {
        view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
        //mVibrator.vibrate(25);

        if (Config.getInstance().getBasicDataBean() == null) {
            Config.getInstance().setBasicDataBean(new BasicDataBean());
        }
        Config.getInstance().getBasicDataBean().setDepartmentList(viewModel.getDepartmentList());

        SelectDialog selectDialog = new SelectDialog(this,
                AppConstants.ACTION_CHOOSE_DEPARTMENT);
        selectDialog.setTitle(getString(R.string.label_select_department));
        selectDialog.setSelectDialogActionListener(new SelectDialog.SelectDialogActionListener() {
            @Override
            public void actionCompletedSuccessfully(int position, String value) {

                llDepartment.setEnabled(false);
                llDepartment.setFocusable(false);
                txtDepartment.setText(value);
                etxtDepartment.setText(viewModel.getDepartmentList().get(position).getDepartmentCode());
                viewModel.setDepartment(viewModel.getDepartmentList().get(position).getDepartmentName());
                viewModel.setDepartmentSelectedPosition(position);
                viewModel.setDepartmentCode(viewModel.getDepartmentList().get(position).getDepartmentCode());
                populateViewModel();
                viewModel.setSectionList(new ArrayList<SectionBean>());
                viewModel.setSectionList(DropDownMenuUtil.getSectionList(viewModel.getDepartmentCode()));

            }

            @Override
            public void actionFailed(String errorMsg) {
                populateViewModel();
            }
        });
        selectDialog.show();

    }

    public void onInternalSalesCorrectionSectionClick(View view) {
        view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
        //mVibrator.vibrate(25);
        if (Config.getInstance().getBasicDataBean() == null) {
            Config.getInstance().setBasicDataBean(new BasicDataBean());
        }
        Config.getInstance().getBasicDataBean().setSectionList(viewModel.getSectionList());

        SelectDialog selectDialog = new SelectDialog(this,
                AppConstants.ACTION_CHOOSE_SECTION);
        selectDialog.setTitle(getString(R.string.label_select_section));
        selectDialog.setSelectDialogActionListener(new SelectDialog.SelectDialogActionListener() {
            @Override
            public void actionCompletedSuccessfully(int position, String value) {

                llSection.setEnabled(false);
                llSection.setFocusable(false);
                txtSection.setText(value);
                etxtSection.setText(viewModel.getSectionList().get(position).getSectionSecCode());
                viewModel.setSection(viewModel.getSectionList().get(position).getSectionName());
                viewModel.setSectionSelectedPosition(position);
                viewModel.setSectionCode(viewModel.getSectionList().get(position).getSectionSecCode());
                populateViewModel();
            }

            @Override
            public void actionFailed(String errorMsg) {
                populateViewModel();
            }
        });
        selectDialog.show();

    }

    public void onInternalSalesCorrectionBackClick(View view) {
        view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
        //mVibrator.vibrate(25);

        if (!viewModel.getInternalSalesCorrectionList().isEmpty()) {
            startActivityForResult(new Intent(getApplicationContext(), FileTransferActivity.class)
                    .putExtra("menuBean", selectedMenuBean)
                    .putExtra("subMenuBean", selectedSubMenuBean), REQ_FILE_TRANSFER);
        } else {
            finish();
        }
    }

    public void onInternalSalesCorrectionModifyClick(View view) {
        view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
        //mVibrator.vibrate(25);

        if (viewModel.getLastEntry() != null) {
            viewModel.setModifyEnabled(true);
            viewModel.setModifiedID(viewModel.getLastEntry().getId());
            viewModel.setData(viewModel.getLastEntry());
            populateViewModel();
        }

    }

    public void onInternalSalesCorrectionDeleteClick(View view) {
        view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
        //mVibrator.vibrate(25);

        if (!viewModel.getInternalSalesCorrectionList().isEmpty() && viewModel.getLastEntry() != null) {
            internalSalesCorrectionRepository.deleteInternalSalesCorrection(
                    viewModel.getLastEntry()
                    , internalSalesCorrectionRepositoryListener);
        }

    }

    public void onInternalSalesCorrectionDepartmentChangeClick(View view) {
        view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
        //mVibrator.vibrate(25);

        llDepartment.setEnabled(true);
        llDepartment.setFocusable(true);
    }

    public void onInternalSalesCorrectionSectionChangeClick(View view) {
        view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
        //mVibrator.vibrate(25);

        llSection.setEnabled(true);
        llSection.setFocusable(true);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            onInternalSalesCorrectionBackClick(toolbar);
            return true;
        }

        if (keyCode == KeyEvent.KEYCODE_MENU) {
            openOptionsMenu();
            return true;
        }
        if (keyCode == KeyEvent.KEYCODE_ENTER) {
            // TODO: 22-07-2018 Do Things On Enter Click
            Toast.makeText(this, "EnterClicked", Toast.LENGTH_SHORT).show();
            return false;
        }
        return false;
    }
}
