package com.phlx.wool.ui.work.harmless;

import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.FrameLayout;

import androidx.lifecycle.ViewModelProviders;

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.BR;
import com.phlx.wool.R;
import com.phlx.wool.data.VMFactory;
import com.phlx.wool.databinding.ActivityHarmlessBinding;
import com.phlx.wool.entity.Label;
import com.phlx.wool.entity.Veterinary;
import com.phlx.wool.ui.base.BaseActivity;
import com.phlx.wool.ui.search.SearchVeterinaryFragment;
import com.phlx.wool.ui.search.interf.IOnSearchVeterinaryClickListener;

import java.util.ArrayList;
import java.util.Date;

/**
 * <pre>
 *     author : lei
 *     e-mail : 18600395998@163.com
 *     time   : 2020/04/21
 *     desc   : 打标页
 *     version: 1.0
 * </pre>
 */
public class HarmlessActivity extends BaseActivity<ActivityHarmlessBinding, HarmlessVM> implements IOnSearchVeterinaryClickListener {

    private ArrayList<Label> labels;
    private TimePickerView pvTime;
    private SearchVeterinaryFragment searchVeterinaryFragment;

    @Override
    public void initParam() {
        super.initParam();
        Bundle mBundle = getIntent().getExtras();
        if (mBundle != null) {
            labels = mBundle.getParcelableArrayList("list");
        }
    }

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

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

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

    @Override
    public void initData() {
        super.initData();
        setSupportActionBar(binding.include.toolbar);
        viewModel.initToolbar();
        viewModel.setLabels(labels);
        initTimePicker();
        searchVeterinaryFragment = SearchVeterinaryFragment.newInstance();
        searchVeterinaryFragment.setIOnSearchVeterinaryClickListener(this);
    }

    @Override
    public void initViewObservable() {

        viewModel.showSearch.observe(this, aBoolean -> {
            searchVeterinaryFragment.showFragment(getSupportFragmentManager(), SearchVeterinaryFragment.TAG);
        });
        /**
         * 发病日期选择器
         */
        viewModel.onsetTime.observe(this, aBoolean -> {
            pvTime.show(binding.onsetDateEdit);
        });

        /**
         * 处理日期
         */
        viewModel.treatmentTime.observe(this, aBoolean -> {
            pvTime.show(binding.treatmentDateTv);
        });


        /**
         * 死亡日期
         */
        viewModel.deathTime.observe(this, aBoolean -> {
            pvTime.show(binding.deathDateEdit);
        });
    }

    @Override
    public void onBackPressed() {
        viewModel.backInventory();
    }

    private void initTimePicker() {//Dialog 模式下，在底部弹出
        pvTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                switch (v.getId()) {
                    case R.id.onset_date_edit:
                        viewModel.setOnsetTime(date);
                        break;
                    case R.id.treatment_date_tv:
                        viewModel.setTreatmentTime(date);
                        break;
                    case R.id.death_date_edit:
                        viewModel.setDeathTime(date);
                        break;
                }
            }
        })
                .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);
            }
        }
    }

    @Override
    public void OnSelectedClick(Veterinary veterinary) {
        viewModel.entity.setVeterinaryName(veterinary.getVeterinaryName());
        viewModel.entity.setVeterinaryCode(veterinary.getVeterinaryCode());
        viewModel.entity.notifyChange();
    }
}
