package com.casic.dcms.widgets;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;

import com.applikeysolutions.cosmocalendar.view.CalendarView;
import com.casic.dcms.R;
import com.pengxh.androidx.lite.kit.DialogKit;
import com.pengxh.androidx.lite.kit.StringKit;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

public class RangeDateActionSheet extends Dialog {

    private static final String TAG = "RangeDateActionSheet";
    private final Context ctx;
    private final OnDateRangeSelectedListener listener;

    public static class Builder {
        private Context context;
        private OnDateRangeSelectedListener listener;

        public Builder setContext(Context context) {
            this.context = context;
            return this;
        }

        public Builder setOnActionSheetListener(OnDateRangeSelectedListener listener) {
            this.listener = listener;
            return this;
        }

        public RangeDateActionSheet build() {
            return new RangeDateActionSheet(this);
        }
    }

    private RangeDateActionSheet(Builder builder) {
        super(builder.context, R.style.UserDefinedActionStyle);
        this.ctx = builder.context;
        this.listener = builder.listener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DialogKit.resetParams(this, Gravity.BOTTOM, R.style.ActionSheetDialogAnimation, 1);
        setContentView(R.layout.range_date_action_sheet);
        setCancelable(true);
        setCanceledOnTouchOutside(true);

        CalendarView calendarView = findViewById(R.id.calendarView);
        Button selectedButton = findViewById(R.id.selectedButton);
        selectedButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<Calendar> selectedDates = calendarView.getSelectedDates();
                if (selectedDates.size() == 0) {
                    StringKit.show(ctx, "请选择正确的日期范围");
                    return;
                }
                Calendar startCalendar = selectedDates.get(0);
                Calendar endCalendar = selectedDates.get(selectedDates.size() - 1);

                if (listener != null) {
                    listener.onDateRangeSelected(getFormatDate(startCalendar), getFormatDate(endCalendar));
                    dismiss();
                } else {
                    Log.e(TAG, "onClick: ", new NullPointerException());
                }
            }
        });
    }

    private String getFormatDate(Calendar calendar) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        return dateFormat.format(calendar.getTime());
    }

    public interface OnDateRangeSelectedListener {
        void onDateRangeSelected(String startDate, String endDate);
    }
}
