package in.techware.carrefour.activity.it;

import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.os.Bundle;
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.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import in.techware.carrefour.R;
import in.techware.carrefour.activity.BaseAppCompatNoDrawerActivity;
import in.techware.carrefour.config.Config;
import in.techware.carrefour.dialogs.SelectDialog;
import in.techware.carrefour.model.BasicDataBean;
import in.techware.carrefour.model.MenuBean;
import in.techware.carrefour.util.AppConstants;
import in.techware.carrefour.util.menuUtils.DropDownMenuUtil;
import in.techware.carrefour.viewModels.SelectPrinterViewModel;

public class SelectPrinterActivity extends BaseAppCompatNoDrawerActivity {

    private static final String TAG = "SelectPrinterA";
    private Button btnBack;
    private Button btnContinue;
    private MenuBean selectedMenuBean;
    private MenuBean selectedSubMenuBean;
    private SelectPrinterViewModel viewModel;
    private CheckBox cbPrinter;
    private LinearLayout llSelectPrinter;
    private TextView txtSelectPrinter;
    private int labelType = 0;

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

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

        viewModel = (SelectPrinterViewModel) ViewModelProviders.of(this).get(SelectPrinterViewModel.class);
        viewModel.setSelectedMenuBean(selectedMenuBean);
        viewModel.setSelectedSubMenuBean(selectedSubMenuBean);
        viewModel.setLabelType(labelType);

        initViews();
//        handleKeyboard();

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

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

        if (viewModel.getPrinterList().isEmpty()) {
            setProgressScreenVisibility(true, true);
            getData(false);
        }

    }

    private void getData(boolean isSwipeRefreshing) {
        viewModel.setPrinterList(DropDownMenuUtil.getPrinterList());
        populateViewModel();
        swipeView.setRefreshing(isSwipeRefreshing);
        setProgressScreenVisibility(false, false);
    }


    @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);
                onSelectPrinterBackClick(toolbar);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void initViews() {

        llSelectPrinter = findViewById(R.id.ll_select_printer);
        txtSelectPrinter = findViewById(R.id.txt_select_printer_name);
        cbPrinter = findViewById(R.id.cb_select_printer);

        btnBack = findViewById(R.id.btn_select_printer_back);
        btnContinue = findViewById(R.id.btn_select_printer_continue);

        llSelectPrinter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onSelectPrinterSelectPrinterClick(v);
            }
        });
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onSelectPrinterBackClick(v);
            }
        });

        btnContinue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onSelectPrinterContinueClick(v);
            }
        });

        cbPrinter.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Log.i(TAG, "onCheckedChanged: CHECKED STATUS : " + isChecked);

                viewModel.setPrinterEnabled(isChecked);
                populateViewModel();
            }
        });
    }

    private void populateViewModel() {

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

        cbPrinter.setChecked(viewModel.isPrinterEnabled());
        if (!viewModel.getPrinterList().isEmpty()) {
            if (viewModel.getPrinterSelectedPosition() != -1 && viewModel.getPrinter() != null) {
                txtSelectPrinter.setText(viewModel.getPrinter().getName());
            }
            llSelectPrinter.setFocusable(viewModel.isPrinterEnabled());
            llSelectPrinter.setEnabled(viewModel.isPrinterEnabled());
        } else {
            llSelectPrinter.setFocusable(false);
            llSelectPrinter.setEnabled(false);
        }
    }


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

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

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

                llSelectPrinter.setEnabled(false);
                llSelectPrinter.setFocusable(false);
                txtSelectPrinter.setText(value);

                viewModel.setPrinter(viewModel.getPrinterList().get(position));
                viewModel.setPrinterSelectedPosition(position);
                populateViewModel();
            }

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

    }

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

        finish();
    }

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

        if (viewModel.isPrinterEnabled() && viewModel.getPrinter() != null) {
            startActivity(new Intent(getApplicationContext(), PrintLabelActivity.class)
                    .putExtra("menuBean", selectedMenuBean)
                    .putExtra("subMenuBean", selectedSubMenuBean)
                    .putExtra("labelType", labelType)
                    .putExtra("printerBean", viewModel.getPrinter()));
            finish();
        } else {
            startActivity(new Intent(getApplicationContext(), PrintLabelActivity.class)
                    .putExtra("menuBean", selectedMenuBean)
                    .putExtra("subMenuBean", selectedSubMenuBean)
                    .putExtra("labelType", labelType));
            finish();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            onSelectPrinterBackClick(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;
    }


}
