package com.wisewoods.eduandroid.view;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.view.TimePickerView;
import com.blankj.utilcode.constant.TimeConstants;
import com.blankj.utilcode.util.TimeUtils;
import com.google.android.material.tabs.TabLayout;
import com.wisewoods.eduandroid.R;
import com.wisewoods.eduandroid.databinding.ViewTimeSelectBinding;

import java.util.Calendar;
import java.util.Date;

public class TimeSelect extends FrameLayout implements TabLayout.OnTabSelectedListener {

    private ViewTimeSelectBinding binding;
    private TimeSelectListener listener;
    private String timeYM;
    private String timeStrStart;
    private String timeStrEnd;

    public interface TimeSelectListener {
        void select(String timeStrStart, String timeStrEnd);
    }

    public TimeSelect(@NonNull Context context) {
        super(context);
    }

    public TimeSelect(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public TimeSelect(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void init() {
        binding = ViewTimeSelectBinding.inflate(LayoutInflater.from(getContext()), this, false);
        addView(binding.getRoot());

        binding.tl.addOnTabSelectedListener(this);
        Date nowDate = TimeUtils.getNowDate();
        timeYM = TimeUtils.date2String(nowDate, "yyyy-MM");
        String num = TimeUtils.date2String(nowDate, "dd");
        setTabData(nowDate, Integer.parseInt(num));


        String nowString = TimeUtils.getNowString(TimeUtils.getSafeDateFormat("yyyy.MM"));
        binding.tvTime.setText(nowString);
        binding.tvTime.setOnClickListener(v -> {
            TimePickerView pvTime = new TimePickerBuilder(getContext(),
                    (date, v1) -> {
                        String dateString = TimeUtils.date2String(date, "yyyy.MM");
                        timeYM = TimeUtils.date2String(date, "yyyy-MM");
                        binding.tvTime.setText(dateString);
                        setTabData(date, 1);
                    })
                    .setType(new boolean[]{true, true, false, false, false, false})// 默认全部显示
                    .build();
            pvTime.show();
        });
    }

    private void setTabData(Date nowDate, int num) {
        Calendar instance = Calendar.getInstance();
        instance.setTime(nowDate);
        instance.set(Calendar.DATE, 1);
        instance.set(Calendar.HOUR, 0);
        instance.set(Calendar.MINUTE, 0);
        instance.set(Calendar.SECOND, 0);
        instance.set(Calendar.MILLISECOND, 0);

        Calendar instance2 = Calendar.getInstance();
        instance2.setTime(nowDate);
        instance2.set(Calendar.DATE, 1);
        instance2.set(Calendar.HOUR, 0);
        instance2.set(Calendar.MINUTE, 0);
        instance2.set(Calendar.SECOND, 0);
        instance2.set(Calendar.MILLISECOND, 0);
        instance2.add(Calendar.MONTH, 1);


        binding.tl.removeAllTabs();
        int postion = 1;
        while (TimeUtils.getTimeSpan(instance.getTime(), instance2.getTime(), TimeConstants.SEC) < 0) {
            View view = View.inflate(getContext(), R.layout.layout_item_tab, null);
            TextView tvWeek = view.findViewById(R.id.tv_week);
            tvWeek.setText(TimeUtils.getChineseWeek(instance.getTime()));
            TextView tvDay = view.findViewById(R.id.tv_day);
            tvDay.setText(TimeUtils.date2String(instance.getTime(), "dd"));
            binding.tl.addTab(binding.tl.newTab().setCustomView(view), postion == num);


            instance.add(Calendar.DATE, 1);
            postion++;
        }

        postDelayed(() ->
                binding.tl.setScrollPosition(
                        num - 1, 0, true), 300);

        timeStrStart = TimeUtils.date2String(nowDate, "yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(nowDate);
        calendar.add(Calendar.DATE, 1);
        timeStrEnd = TimeUtils.date2String(instance.getTime(), "yyyy-MM-dd");
    }


    @Override
    public void onTabSelected(TabLayout.Tab tab) {

        LinearLayout llTab = (LinearLayout) tab.getCustomView();
        TextView tvWeek = llTab.findViewById(R.id.tv_week);
        TextView tvDay = llTab.findViewById(R.id.tv_day);
        tvWeek.setTextColor(Color.parseColor("#333333"));
        tvDay.setTextColor(Color.parseColor("#ffffff"));
        tvDay.setBackground(getResources().getDrawable(R.drawable.shape_date_select));

        if (listener != null) {
            timeStrStart = timeYM + "-" + tvDay.getText().toString().trim();
            Date date = TimeUtils.string2Date(timeStrStart, "yyyy-MM-dd");
            Calendar instance = Calendar.getInstance();
            instance.setTime(date);
            instance.add(Calendar.DATE, 1);
            timeStrEnd = TimeUtils.date2String(instance.getTime(), "yyyy-MM-dd");
            listener.select(timeStrStart, timeStrEnd);
        }

    }

    public String getTimeStrStart() {
        return timeStrStart;
    }

    public String getTimeStrEnd() {
        return timeStrEnd;
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {
        LinearLayout llTab = (LinearLayout) tab.getCustomView();
        TextView tvWeek = llTab.findViewById(R.id.tv_week);
        TextView tvDay = llTab.findViewById(R.id.tv_day);
        tvWeek.setTextColor(Color.parseColor("#999999"));
        tvDay.setTextColor(Color.parseColor("#999999"));
        tvDay.setBackground(null);
    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    public void setTimeListener(TimeSelectListener listener) {
        this.listener = listener;
    }
}
