package com.phlx.wool.ui.villus;

import android.app.Dialog;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.SpinnerAdapter;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModelProviders;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectChangeListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.phlx.wool.Configs;
import com.phlx.wool.R;
import com.phlx.wool.data.VMFactory;
import com.phlx.wool.databinding.ActivityVillusBuyBinding;
import com.phlx.wool.ui.base.BaseActivity;
import com.phlx.wool.ui.main.MainActivity;
import com.phlx.wool.ui.villus.vm.VillusBuyVM;
import com.printer.sdk.utils.PrefUtils;

import java.util.Date;

import me.tatarka.bindingcollectionadapter2.BR;

public class VillusBuyActivity extends BaseActivity<ActivityVillusBuyBinding, VillusBuyVM> {

    private TimePickerView pvTime;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_villus_buy;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initParam() {
        super.initParam();
    }

    @Override
    public VillusBuyVM initViewModel() {
        VMFactory factory = VMFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(VillusBuyVM.class);
    }

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

        viewModel.initToolbar();
        initTimePicker();
    }

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

        viewModel.buyDay.observe(this, aBoolean -> {
            pvTime.show(binding.birthDayTv);
        });

        viewModel.changeDivision.observe(this, aBoolean -> {
            binding.divisionSp.setAdapter(viewModel.getAdapter(VillusBuyActivity.this));
        });

        //弹出绒毛购买打印选择
        viewModel.printEvent.observe(this, aBoolean -> {
            new MaterialDialog.Builder(this).title("系统信息").content("是否打印")
                    .cancelable(false).canceledOnTouchOutside(false)
                    .positiveText("确定").onPositive(new MaterialDialog.SingleButtonCallback() {
                @Override
                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                    if (which == DialogAction.POSITIVE) {
                        if (Configs.isConnected)
                            dialog.dismiss();
                        viewModel.printVillusBuy();
                    }
                }
            }).negativeText("取消").onNegative(new MaterialDialog.SingleButtonCallback() {
                @Override
                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                    if (which == DialogAction.NEGATIVE) {
                        dialog.dismiss();
                        viewModel.backMain();
                    }
                }
            }).show();
        });

    }

    private void initTimePicker() {//Dialog 模式下，在底部弹出
        pvTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                viewModel.setBuyDate(date);
            }
        })
                .setTimeSelectChangeListener(new OnTimeSelectChangeListener() {
                    @Override
                    public void onTimeSelectChanged(Date date) {
                    }
                })
                .setType(new boolean[]{true, true, true, false, false, false})
                .isDialog(true) //默认设置false ，内部实现将DecorView 作为它的父控件。
                .addOnCancelClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Log.i("pvTime", "onCancelClickListener");
                    }
                })
                .setItemVisibleCount(5) //若设置偶数，实际值会加1（比如设置6，则最大可见条目为7）
                .setLineSpacingMultiplier(2.0f)
                .isAlphaGradient(true)
                .build();

        Dialog mDialog = pvTime.getDialog();
        if (mDialog != null) {

            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.BOTTOM);

            params.leftMargin = 0;
            params.rightMargin = 0;
            pvTime.getDialogContainerLayout().setLayoutParams(params);

            Window dialogWindow = mDialog.getWindow();
            if (dialogWindow != null) {
                dialogWindow.setWindowAnimations(com.bigkoo.pickerview.R.style.picker_view_slide_anim);//修改动画样式
                dialogWindow.setGravity(Gravity.BOTTOM);//改成Bottom,底部显示
                dialogWindow.setDimAmount(0.3f);
            }
        }
    }

}
