package in.techware.carrefour.activity.it;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.view.HapticFeedbackConstants;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.Toast;

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.dbUtils.repositories.PrintLabelRepository;
import in.techware.carrefour.model.MenuBean;
import in.techware.carrefour.model.PrintLabelBean;
import in.techware.carrefour.util.AppConstants;

public class SelectLabelTypeActivity extends BaseAppCompatNoDrawerActivity {

    private static final String TAG = "SelectLabelTypeA";
    private Button btnBack;
    private Button btnContinue;
    private MenuBean selectedMenuBean;
    private MenuBean selectedSubMenuBean;
    private RadioGroup rbgLabelType;
    private int labelType;
    private PrintLabelRepository printLabelRepository;
    private PrintLabelRepository.PrintLabelRepositoryListener printLabelRepositoryListener;

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

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

        initViews();

        if (getSupportActionBar() != null) {
            setTitle(R.string.title_print_label, 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
                        || data.getIntExtra("result", 0) == AppConstants.FILE_TRANSFER_RESULT_CONTINUE) {
//                    startActivity(new Intent(SelectLabelTypeActivity.this, SelectPrinterActivity.class)
                    startActivity(new Intent(SelectLabelTypeActivity.this, PrintLabelActivity.class)
                            .putExtra("menuBean", selectedMenuBean)
                            .putExtra("subMenuBean", selectedSubMenuBean)
                            .putExtra("labelType", labelType));
                }
            }
        }
    }

    private void initViews() {

        rbgLabelType = findViewById(R.id.rbg_select_label_type);

        btnBack = findViewById(R.id.btn_select_label_type_back);
        btnContinue = findViewById(R.id.btn_select_label_type_continue);

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

        setPrintLabelRepositoryListener();
    }

    private void setPrintLabelRepositoryListener() {
        printLabelRepository = PrintLabelRepository.getInstance();
        printLabelRepositoryListener = new PrintLabelRepository.PrintLabelRepositoryListener() {
            @Override
            public void onAddCompleted(PrintLabelBean printLabelBean) {

            }

            @Override
            public void onAllListLoaded(ArrayList<PrintLabelBean> printLabelList) {

            }

            @Override
            public void onAllSentListLoaded(int labelType, ArrayList<PrintLabelBean> printLabelList) {

            }

            @Override
            public void onAllUnsentListLoaded(int labelType, ArrayList<PrintLabelBean> printLabelList) {
                if (!printLabelList.isEmpty()) {
                    startActivityForResult(new Intent(getApplicationContext(), FileTransferActivity.class)
                            .putExtra("menuBean", selectedMenuBean)
                            .putExtra("subMenuBean", selectedSubMenuBean)
                            .putExtra("labelType", labelType), REQ_FILE_TRANSFER);
                } else {
//                    startActivity(new Intent(SelectLabelTypeActivity.this, SelectPrinterActivity.class)
                    startActivity(new Intent(SelectLabelTypeActivity.this, PrintLabelActivity.class)
                            .putExtra("menuBean", selectedMenuBean)
                            .putExtra("subMenuBean", selectedSubMenuBean)
                            .putExtra("labelType", labelType));
                }
            }

            @Override
            public void onDeleteAllCompleted() {

            }

            @Override
            public void onDeleteAllSentCompleted(String key, int labelType) {

            }

            @Override
            public void onDeleteAllUnsentCompleted(int labelType) {

            }

            @Override
            public void onDeleteCompleted(PrintLabelBean printLabelBean) {

            }

            @Override
            public void onFailed() {

            }

            @Override
            public void onUpdateCompleted(PrintLabelBean printLabelBean) {

            }

            @Override
            public void onPrintLabelBeanLoaded(PrintLabelBean printLabelBean) {

            }
        };


    }

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

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

        switch (rbgLabelType.getCheckedRadioButtonId()) {
            case R.id.rb_select_label_type_inventory_label:
                labelType = AppConstants.LABEL_TYPE_INVENTORY_LABEL;
                break;
            case R.id.rb_select_label_type_it_label:
                labelType = AppConstants.LABEL_TYPE_IT_LABEL;
                break;
            case R.id.rb_select_label_type_shelf_label:
                labelType = AppConstants.LABEL_TYPE_SHELF_LABEL;
                break;
            default:
                labelType = -1;
                break;
        }

        if (labelType != -1) {
            printLabelRepository.getAllUnsentPrintLabels(labelType, printLabelRepositoryListener);
        } else {
            Snackbar.make(coordinatorLayout, R.string.message_please_select_a_label_type, Snackbar.LENGTH_LONG)
                    .setAction(R.string.btn_dismiss, snackBarDismissOnClickListener).show();
        }

    }

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