package com.philisense.rdgo.ui.user;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;

import com.borax12.materialdaterangepicker.date.DatePickerDialog;
import com.example.xrecyclerview.XRecyclerView;
import com.philisense.rdgo.R;
import com.philisense.rdgo.adapter.OrderAdapter;
import com.philisense.rdgo.adapter.QuareAdapter;
import com.philisense.rdgo.base.BaseActivity;
import com.philisense.rdgo.bean.LineBean;
import com.philisense.rdgo.bean.OError;
import com.philisense.rdgo.bean.response.AllBoxBean;
import com.philisense.rdgo.bean.response.PutOutRecordBean;
import com.philisense.rdgo.databinding.ActivityOrderBinding;
import com.philisense.rdgo.databinding.ActivityQuareBinding;
import com.philisense.rdgo.http.RequestImpl;
import com.philisense.rdgo.viewModel.OrderViewModel;
import com.philisense.rdgo.viewModel.QuareViewModel;
import com.scandecode.ScanDecode;
import com.scandecode.inf.ScanInterface;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class QuareActivity extends BaseActivity<ActivityQuareBinding> implements  DatePickerDialog.OnDateSetListener {

    private QuareViewModel viewModel;
    private QuareAdapter adapter;


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

        Toolbar toolbar = bindingView.toolbar;
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                finish();
            }
        });




        bindingView.quareRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new QuareAdapter(this);
        bindingView.quareRecyclerView.setAdapter(adapter);

        viewModel = new QuareViewModel(this);





        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                Calendar now = Calendar.getInstance();
                DatePickerDialog dpd = DatePickerDialog.newInstance(
                        QuareActivity.this,
                        now.get(Calendar.YEAR),
                        now.get(Calendar.MONTH),
                        now.get(Calendar.DAY_OF_MONTH)
                );
                dpd.setStartTitle("开始时间");
                dpd.setEndTitle("结束时间");
                dpd.show(getFragmentManager(), "Datepickerdialog");
                return true;
            }
        });

        getPutOutInfo(false, true, 0);
        getLin();

        bindingView.quareRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                getPutOutInfo(false, true, 1);
            }

            @Override
            public void onLoadMore() {

//                getPutOutInfo(true,false,1);
                bindingView.quareRecyclerView.refreshComplete();
            }
        });


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.search_menu, menu);
        return true;
    }



    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }


    /**
     * 获取出库记录
     */
    public void getPutOutInfo(boolean isNextPage, boolean isRefresh, int flag) {



        viewModel.getPutInOrder(isNextPage, isRefresh, flag, new RequestImpl<AllBoxBean>() {
            @Override
            public void loadSuccess(AllBoxBean bean) {
                adapter.setData(bean.getBoxIn());
                bindingView.quareRecyclerView.refreshComplete();
                getNum(bean.getBoxIn());
                getNowTime();
                showContentView();
                bindingView.quareRecyclerView.isnomore = bean.getBoxIn().size() % 20 != 0;

            }

            @Override
            public void loadFailed(OError error) {
                showContentView();
                adapter.setData(new ArrayList<>());
                bindingView.quareRecyclerView.refreshComplete();
                Snackbar.make(bindingView.orderRootLayout, error.getErrorMsg(), Snackbar.LENGTH_LONG).show();
                getNowTime();
                bindingView.num.setText("总共0箱");
            }
        });

    }

    private void getNum( List<String> ss) {
        int a = ss.size();

        bindingView.num.setText("总共" + a + "箱");
    }

    private void getNowTime() {

        bindingView.time.setText(viewModel.startTime + " 至 " + viewModel.endTime);


    }

    private List<LineBean.ListBean> myline;

    /**
     * 获取产线列表
     */
    public void getLin() {
        myline = new ArrayList<>();
        viewModel.getLins(new RequestImpl<LineBean>() {
            @Override
            public void loadSuccess(LineBean bean) {

                myline = bean.getList();

            }

            @Override
            public void loadFailed(OError error) {

            }
        });


        bindingView.lins.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                digo(myline);
            }
        });


    }

    private void digo(List<LineBean.ListBean> bean) {
        if (bean == null) {
            bean = new ArrayList<>();
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(QuareActivity.this);
        builder.setTitle("选择需要查询的产线");

        /**
         * 设置内容区域为简单列表项
         */
        String[] Items = new String[bean.size() + 1];

        Items[0] = "全部";
        for (int i = 0; i < bean.size(); i++) {
            Items[i + 1] = bean.get(i).getName();
        }

        builder.setItems(Items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

                if (i == 0) {
                    viewModel.lin = "";
                } else {
                    viewModel.lin = Items[i];
                }

                bindingView.lins.setText(Items[i] + "");
                showLoading();
                getPutOutInfo(false, false, 1);

            }
        });

        builder.setCancelable(true);
        AlertDialog dialog = builder.create();
        dialog.show();
    }


    public static void start(Context context) {
        Intent intent = new Intent(context, QuareActivity.class);
        ActivityCompat.startActivity(context, intent, null);
    }

    @Override
    public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth, int yearEnd, int monthOfYearEnd, int dayOfMonthEnd) {
        int month = monthOfYear + 1;
        String mfy = "";
        if (month < 10) {
            mfy = "0" + month;
        } else {
            mfy = month + "";
        }
        int monthEnd = monthOfYearEnd + 1;
        String mfe = "";
        if (month < 10) {
            mfe = "0" + monthEnd;
        } else {
            mfe = monthEnd + "";
        }
        String day = "";
        if (dayOfMonth < 10) {
            day = "0" + dayOfMonth;
        } else {
            day = dayOfMonth + "";
        }

        String dayEnd = "";
        if (dayOfMonthEnd < 10) {
            dayEnd = "0" + dayOfMonthEnd;
        } else {
            dayEnd = dayOfMonthEnd + "";
        }

        viewModel.startTime = year + "-" + mfy + "-" + day;
        viewModel.endTime = yearEnd + "-" + mfe + "-" + dayEnd;
        showLoading();
        getPutOutInfo(false, false, 1);
    }


}
