package com.sjkj.base_lib.utils;

import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.CustomListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.contrarywind.view.WheelView;
import com.sjkj.base_lib.R;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeViewUtils {
    private Context context;
    private TimePickerBuilder timePickerBuilder;
    private TimePickerView timePickerView;
    public static final int TYPE_TIME = 0;
    public static final int TYPE_DATE = 1;
    public static final int TYPE_MONTH = 2;
    public static final int TYPE_YEAR = 3;
    private int type = 0;

    public TimeViewUtils(Context context, int type, OnTimeListener timeListener) {
        this.context = context;
        this.type = type;
        this.timeListener = timeListener;
        initTimeView(context);
    }

    public void initTimeView(Context context) {
        String formatStr = "";
        if (type == TYPE_TIME) {
            formatStr = "yyyy-MM-dd HH:mm:ss";
        } else if (type == TYPE_DATE) {
            formatStr = "yyyy-MM-dd";
        } else if (type == TYPE_MONTH) {
            formatStr = "yyyy-MM";
        } else if (type == TYPE_YEAR) {
            formatStr = "yyyy";
        }
        SimpleDateFormat formatter = new SimpleDateFormat(formatStr);
        Calendar selectedDate = Calendar.getInstance();//系统当前时间
        timePickerBuilder = new TimePickerBuilder(context, new OnTimeSelectListener() {
            public void onTimeSelect(final Date date, View v) {
                String choiceTime = formatter.format(date);//日期 String
//                binding.tvTime.setText(choiceTime);
//                viewModel.time.set(choiceTime);
                if (timeListener != null)
                    timeListener.timeSelect(choiceTime);
            }

        });
        timePickerBuilder.setDate(selectedDate);
        timePickerBuilder.setLineSpacingMultiplier(1.8f);
        if (AppTools.isGhb()) {
            timePickerBuilder.setContentTextSize(24);
        }
        timePickerBuilder.setOutSideCancelable(true);//点击屏幕，点在控件外部范围时，是否取消显示
        timePickerBuilder.setLayoutRes(AppTools.isGhb() ? R.layout.include_picker_time_big : R.layout.include_picker_time, new CustomListener() {

            @Override
            public void customLayout(View v) {
                TextView tvSubmit = (TextView) v.findViewById(R.id.tv_finish);
                TextView ivCancel = (TextView) v.findViewById(R.id.tv_cancel);
                TextView title = (TextView) v.findViewById(R.id.tv_title);
                if (AppTools.isGhb()) {
                    WheelView year = (WheelView) v.findViewById(R.id.year);
                    WheelView month = (WheelView) v.findViewById(R.id.month);
                    WheelView day = (WheelView) v.findViewById(R.id.day);
                    WheelView hour = (WheelView) v.findViewById(R.id.hour);
                    WheelView min = (WheelView) v.findViewById(R.id.min);
                    WheelView second = (WheelView) v.findViewById(R.id.second);
                    year.setTextSize(20);
                    month.setTextSize(20);
                    day.setTextSize(20);
                    hour.setTextSize(20);
                    min.setTextSize(20);
                    second.setTextSize(20);
                    if (type == TYPE_DATE) {
                        //年月日(防止大字版 三个年月日不居中的问题)
                        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) year.getLayoutParams();
                        params.weight = 1;
                        year.setLayoutParams(params);

                    }
                }

                title.setText("请选择时间");
                tvSubmit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (timePickerView != null) {
                            timePickerView.returnData();
                            timePickerView.dismiss();
                        }
                    }
                });
                ivCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (timePickerView != null) {
                            timePickerView.dismiss();
                        }
                    }
                });
            }
        });

        //  .setDate(selectedDate)//设置系统时间为当前时间
        //  .setRangDate(startDate, endDate)//设置控件日期范围 也可以不设置默认1900年到2100年
        if (type == TYPE_TIME) {
            timePickerBuilder.setType(new boolean[]{true, true, true, true, true, false});//设置年月日时分秒是否显示 true:显示 false:隐藏
        } else if (type == TYPE_DATE) {
            timePickerBuilder.setType(new boolean[]{true, true, true, false, false, false});//设置年月日时分秒是否显示 true:显示 false:隐藏
        } else if (type == TYPE_MONTH) {
            timePickerBuilder.setType(new boolean[]{true, true, false, false, false, false});//设置年月日时分秒是否显示 true:显示 false:隐藏
        } else if (type == TYPE_YEAR) {
            timePickerBuilder.setType(new boolean[]{true, false, false, false, false, false});//设置年月日时分秒是否显示 true:显示 false:隐藏
        }
        timePickerBuilder.setLabel("年", "月", "日", "时", "分", "秒");
        timePickerBuilder.isCenterLabel(false); //是否只显示中间选中项的label文字，false则每项item全部都带有label。
        timePickerBuilder.setDividerColor(0xFFD2D2D2);//设置分割线颜色
        timePickerBuilder.isCyclic(false);//是否循环显示日期 例如滑动到31日自动转到1日 有个问题：不能实现日期和月份联动
        timePickerView = timePickerBuilder.build();
        timePickerView.show();
    }

    public interface OnTimeListener {
        void timeSelect(String time);
    }

    private OnTimeListener timeListener;

}
