package com.wisewoods.eduandroid.view;

import android.content.Context;
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.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.ViewTimeSelect2Binding;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class TimeSelect2 extends FrameLayout implements TabLayout.OnTabSelectedListener {

    private ViewTimeSelect2Binding binding;
    private TimeSelectListener listener;
    private String timeStrStart = "";
    private String timeStrEnd = "";
    private List<String> dateList;

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

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

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

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

    public void setDate(String startTime, String endTime) {
        binding.tvTitle.setText(startTime + " — " + endTime);
        setTabData(startTime, endTime);

    }

    private void setTabData(String startTime, String endTime) {
        Date startDate = TimeUtils.string2Date(startTime, "yyyy年MM月dd日");
        Date endDate = TimeUtils.string2Date(endTime, "yyyy年MM月dd日");
        Calendar instance = Calendar.getInstance();
        instance.setTime(startDate);
        Calendar instance2 = Calendar.getInstance();
        instance2.setTime(endDate);

        binding.tl.removeAllTabs();
        View viewAll = View.inflate(getContext(), R.layout.layout_item_date, null);
        TextView tvAll = viewAll.findViewById(R.id.tv);
        tvAll.setText("全部");
        tvAll.setTextColor(getResources().getColor(R.color.white));
        tvAll.setBackgroundResource(R.drawable.shape_date_btn);
        binding.tl.addTab(binding.tl.newTab().setCustomView(viewAll));
        dateList = new ArrayList<>();
        int postion = 1;
        while (TimeUtils.getTimeSpan(instance.getTime(), instance2.getTime(), TimeConstants.SEC) <= 0) {
            View view = View.inflate(getContext(), R.layout.layout_item_date, null);
            TextView tv = view.findViewById(R.id.tv);

            String chineseWeek = TimeUtils.getChineseWeek(instance.getTime());
            String dd = TimeUtils.date2String(instance.getTime(), "dd");

            tv.setText(chineseWeek + "\n" + dd);
            tv.setTextColor(getResources().getColor(R.color.gary999));
            tv.setBackgroundResource(R.drawable.shape_dialog_edit);

            binding.tl.addTab(binding.tl.newTab().setCustomView(view));
            dateList.add(TimeUtils.date2String(instance.getTime(), "yyyy-MM-dd"));

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

    private void init() {
        binding = ViewTimeSelect2Binding.inflate(LayoutInflater.from(getContext()), this, false);
        addView(binding.getRoot());
        binding.tl.addOnTabSelectedListener(this);
    }


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

        LinearLayout llTab = (LinearLayout) tab.getCustomView();
        TextView tv = llTab.findViewById(R.id.tv);
        tv.setTextColor(getResources().getColor(R.color.white));
        tv.setBackgroundResource(R.drawable.shape_date_btn);
        int position = tab.getPosition();
        if (position == 0) {
            timeStrStart = "";
            timeStrEnd = "";
        } else {
            timeStrStart = dateList.get(position - 1);
            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");
        }

        if (listener != null) {
            listener.select(timeStrStart, timeStrEnd);
        }

    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {
        LinearLayout llTab = (LinearLayout) tab.getCustomView();
        TextView tv = llTab.findViewById(R.id.tv);
        tv.setTextColor(getResources().getColor(R.color.gary999));
        tv.setBackgroundResource(R.drawable.shape_dialog_edit);
    }

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

    }

    public String getTimeStrStart() {
        return timeStrStart;
    }

    public String getTimeStrEnd() {
        return timeStrEnd;
    }


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