package com.bc_chat.mine.window;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomSheetDialogFragment;
import android.view.Gravity;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.bc_chat.mine.R;
import com.bc_chat.mine.databinding.FragmentTimeHourMinuteBinding;
import com.zhaohaoting.framework.abs.AbsDialogFragment;
import com.zhaohaoting.framework.abs.contract.BaseContract;
import com.zhaohaoting.framework.view.picker.base.util.DateUtils;
import com.zhaohaoting.framework.view.picker.framework.widget.WheelView;

import java.util.ArrayList;

/**
 * author：Created by zhaohaoting on 2019/5/15
 * email：526309416@qq.com
 * desc： 时分
 */
public class HourMinuteDialogFragment extends AbsDialogFragment {

    private FragmentTimeHourMinuteBinding binding;
    private ArrayList<String> startHours = new ArrayList<>();
    private ArrayList<String> endHours = new ArrayList<>();
    private static final int MinHour = 1;
    private static final int MaxHour = 24;
    private int selectedStartHourIndex = 0;
    private int selectedEndHourIndex = 0;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(BottomSheetDialogFragment.STYLE_NORMAL, R.style.CustomBottomSheetDialogTheme);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.BOTTOM;
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    @Override
    protected BaseContract.BasePresenter initPresenter() {
        return null;
    }

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_time_hour_minute;
    }

    public interface OnSelectDateListener {
        void onSelectDate(String hour, String minute);
    }

    private OnSelectDateListener selectDateListener;

    public void setSelectDateListener(OnSelectDateListener selectDateListener) {
        this.selectDateListener = selectDateListener;
    }

    @Override
    protected void initialize() {
        binding = (FragmentTimeHourMinuteBinding) rootDataBinding;
        WheelView.LineConfig lineConfig = new WheelView.LineConfig();
        lineConfig.setVisible(false);
        initStartHours(MinHour, MaxHour);
        initEndHours(MinHour, MaxHour);
        binding.wvHour.setTextSize(16, 14);
        binding.wvHour.setTextColor(getResources().getColor(R.color.black_99_color), getResources().getColor(R.color.black_33_color));
        binding.wvHour.setOffset(1);
        binding.wvHour.setItemHeight(50);
        binding.wvHour.setLineConfig(lineConfig);
        binding.wvHour.setCycleDisable(true);
        binding.wvHour.setItems(startHours, selectedStartHourIndex);
        binding.wvHour.setOnWheelListener((isUserScroll, index, item) -> {//时分
            selectedStartHourIndex = index;
            int minHour = DateUtils.trimZero(getSelectedStartHour());
            initEndHours(minHour + 1, minHour + 24);
            if (endHours.size() <= selectedEndHourIndex) {
                selectedEndHourIndex = endHours.size() - 1;
            }
            binding.wvMinute.setItems(endHours, selectedEndHourIndex);
        });
        binding.wvMinute.setAdapter(new HourAdapter());
        binding.wvMinute.setTextSize(16, 14);
        binding.wvMinute.setTextColor(getResources().getColor(R.color.black_99_color), getResources().getColor(R.color.black_33_color));
        binding.wvMinute.setOffset(1);
        binding.wvMinute.setItemHeight(50);
        binding.wvMinute.setLineConfig(lineConfig);
        binding.wvMinute.setCycleDisable(true);
        binding.wvMinute.setItems(endHours, selectedEndHourIndex);
        binding.wvMinute.setOnWheelListener((isUserScroll, index, item) -> {//时分
            selectedEndHourIndex = index;
        });

        binding.tvConfirm.setOnClickListener(v -> {
            if (selectDateListener != null) {
                String selectedStartHour = getSelectedStartHour();
                String selectedEndHour = getSelectedEndHour();
                selectDateListener.onSelectDate(selectedStartHour, selectedEndHour);
                dismissAllowingStateLoss();
            }
        });
        binding.tvCancel.setOnClickListener(v -> dismissAllowingStateLoss());

    }


    /**
     * 初始化时间选择器数据 00:00 -23:59
     */
    private void initStartHours(int minHour, int maxHour) {
        startHours.clear();
        for (int i = minHour; i <= maxHour; i++) {
            StringBuilder sbHour = new StringBuilder();
            if (i < 10) {
                sbHour.append(0).append(i);
            } else {
                sbHour.append(i);
            }
            startHours.add(getString(R.string._hour, sbHour.toString()));
        }
    }

    /**
     * 初始化时间选择器数据 00:00 -23:59
     */
    private void initEndHours(int minHour, int maxHour) {
        endHours.clear();
        for (int i = minHour; i <= maxHour; i++) {
            StringBuilder sbMinute = new StringBuilder();
//            int hour;
////            if (i >= 24) {
////                hour = i - 24;
////            } else {
////                hour = i;
////            }
            if (i < 10) {
                sbMinute.append(0).append(i);
            } else {
                sbMinute.append(i);
            }
            endHours.add(getString(R.string._hour, sbMinute.toString()));
        }
    }

    public String getSelectedStartHour() {
        if (startHours.size() <= selectedStartHourIndex) {
            selectedStartHourIndex = startHours.size() - 1;
        }
        String hour = startHours.get(selectedStartHourIndex);
        return hour.substring(0, 2);
    }

    public String getSelectedEndHour() {
        if (endHours.size() <= selectedEndHourIndex) {
            selectedEndHourIndex = endHours.size() - 1;
        }
        String minute = endHours.get(selectedEndHourIndex);
        return minute.substring(0, 2);
    }

    class HourAdapter extends WheelView.WheelAdapter {
        @Override
        public String getText(String string) {
            int hour = DateUtils.trimZero(string.substring(0, 2));
            if (hour >= 24) {
                hour = hour - 24;
            }
            StringBuilder sbMinute = new StringBuilder();
            if (hour < 10) {
                sbMinute.append(0).append(hour);
            } else {
                sbMinute.append(hour);
            }
            return getString(R.string._hour, sbMinute.toString());
        }
    }
}
