package com.philisense.rdgo.ui.Query;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Toast;

import com.example.xrecyclerview.XRecyclerView;
import com.philisense.rdgo.R;
import com.philisense.rdgo.adapter.QuareAdapter;
import com.philisense.rdgo.bean.OError;
import com.philisense.rdgo.bean.response.BoxBean;
import com.philisense.rdgo.bean.response.PileInfoBean;
import com.philisense.rdgo.databinding.ActivityQueryNextBinding;

import com.philisense.rdgo.http.RequestImpl;
import com.philisense.rdgo.viewModel.QueryViewModel;
import com.philisense.rdgo.viewModel.ResetViewModel;
import com.scandecode.ScanDecode;
import com.scandecode.inf.ScanInterface;

public class QueryNextActivity extends AppCompatActivity {

    public ActivityQueryNextBinding mBinding;
    private ScanDecode scanDecode;
    private QueryViewModel viewModel;

    private QuareAdapter adapter;
    private String id = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_query_next);
        Toolbar toolbar = mBinding.include.toolbar;
        toolbar.setTitle("详细信息");
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                finish();
            }
        });
        if (getIntent() != null) {
            id = getIntent().getStringExtra("orderId");
        }

        mBinding.id.setText(id);
        mBinding.boxRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new QuareAdapter(this);
        mBinding.boxRecyclerView.setAdapter(adapter);
        viewModel = new QueryViewModel(this);
        mBinding.boxRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mBinding.boxRecyclerView.refreshComplete();
            }

            @Override
            public void onLoadMore() {
                mBinding.boxRecyclerView.refreshComplete();
            }
        });


        getBox(id);

    }

    private void getBox(String id) {
        final ProgressDialog dialog = ProgressDialog.show(QueryNextActivity.this, "提示", "正在获取数据..");
        viewModel.getBoxId(id, new RequestImpl<BoxBean>() {
            @Override
            public void loadSuccess(BoxBean bean) {
                if (dialog != null) {
                    dialog.dismiss();
                }
                if (bean != null) {

                    mBinding.setProduct(bean.getData());
                    int gene = bean.getData().getGene();
                    if (gene == 0) {
                        mBinding.Gene.setText("是");
                    } else if (gene == 1) {
                        mBinding.Gene.setText("否");
                    } else {
                        mBinding.Gene.setText("其他");
                    }
                    get(bean.getData().getBoxId());

                }
            }

            @Override
            public void loadFailed(OError error) {
                if (dialog != null) {
                    dialog.dismiss();
                }
                Snackbar.make(mBinding.putInRootLayout, error.getErrorMsg(), Snackbar.LENGTH_LONG).show();

            }
        });
    }

    public void get(String boxId) {

        viewModel.getPileInfo(boxId, new RequestImpl<PileInfoBean>() {
            @Override
            public void loadSuccess(PileInfoBean bean) {

                if (bean != null) {
                    adapter.setData(bean.getData().getBoxIn());
                    mBinding.boxRecyclerView.refreshComplete();
                    mBinding.num.setText(bean.getData().getBoxNum() + "");
                    mBinding.line.setText(bean.getData().getLine());
                    mBinding.confirmer.setText(bean.getData().getConfirmer());
                    mBinding.warehouse.setText(bean.getData().getWarehouse());
                    mBinding.groupCode.setText(bean.getData().getGroupCode());
                    mBinding.code.setText(bean.getData().getTypeCode());
                    int status = bean.getData().getStatus();
                    if (status == 0) {
                        mBinding.status.setText("在库");
                    } else {
                        mBinding.status.setText("不在库");
                    }

                    if ("".equals(mBinding.level.getText().toString().trim())) {
                        mBinding.level.setText(bean.getData().getLevel());
                    }

                }


            }

            @Override
            public void loadFailed(OError error) {

                Snackbar.make(mBinding.putInRootLayout, error.getErrorMsg(), Snackbar.LENGTH_LONG).show();

            }
        });


    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        try {
            scanDecode.onDestroy();

        } catch (Exception e) {
        }
    }

    /**
     * @param context activity
     */
    public static void start(Activity context, String orderId) {
        Intent intent = new Intent(context, QueryNextActivity.class);
        intent.putExtra("orderId", orderId);
        ActivityCompat.startActivity(context, intent, null);
    }
}
