package com.casic.dcms.widgets.datepicker;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.casic.dcms.R;
import com.casic.dcms.utils.CalendarUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

@SuppressLint("SetTextI18n")
public class DatePopupWindow extends PopupWindow {
    private static final String TAG = "DatePopupWindow";
    private View rootView;
    private QMUIRoundButton selectDoneButton;
    private RecyclerView dateRecyclerView;
    private TextView hintTextTextView;
    private TextView endWeekView;
    private TextView endDateView;
    private LinearLayout endDateLayout;
    private TextView sumTimeView;
    private TextView startWeekView;
    private TextView startDateView;

    private boolean dayFlag;
    private Activity activity;
    private Date mSetDate;
    private String currentDate;
    private String startDesc;
    private String endDesc;
    private int startGroupPosition = -1;
    private int endGroupPosition = -1;
    private int startChildPosition = -1;
    private int endChildPosition = -1;
    private int childPosition = -1;//当天在列表中的子索引
    private DateAdapter mDateAdapter;
    private List<DateInfo> mList;
    private DateOnClickListener mOnClickListener;

    private DatePopupWindow(Builder builder) {
        this.activity = builder.activity;
        this.currentDate = builder.date;
        this.startDesc = builder.startDesc;
        this.endDesc = builder.endDesc;
        this.dayFlag = builder.dayFlag;
        this.startGroupPosition = builder.startGroupPosition;
        this.startChildPosition = builder.startChildPosition;
        this.endGroupPosition = builder.endGroupPosition;
        this.endChildPosition = builder.endChildPosition;
        this.mOnClickListener = builder.mOnClickListener;

        LayoutInflater inflater = (LayoutInflater) this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rootView = inflater.inflate(R.layout.popu_date, null);
        this.setContentView(rootView);
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setAnimationStyle(R.style.dialogWindowAnim);
        this.setFocusable(true);
        this.setBackgroundDrawable(new BitmapDrawable());
        this.setOnDismissListener(new ShareDismissListener());
        backgroundAlpha(activity);

        initView();
        setInitSelect();
        create(builder.parentView);
    }

    private void backgroundAlpha(Activity context) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = 1.0f; //0.0-1.0
        context.getWindow().setAttributes(lp);
    }

    private void initView() {
        selectDoneButton = rootView.findViewById(R.id.selectDoneButton);
        dateRecyclerView = rootView.findViewById(R.id.dateRecyclerView);
        hintTextTextView = rootView.findViewById(R.id.hintTextTextView);
        endWeekView = rootView.findViewById(R.id.endWeekView);
        endDateView = rootView.findViewById(R.id.endDateView);
        endDateLayout = rootView.findViewById(R.id.endDateLayout);
        sumTimeView = rootView.findViewById(R.id.sumTimeView);
        startWeekView = rootView.findViewById(R.id.startWeekView);
        startDateView = rootView.findViewById(R.id.startDateView);
        TextView clearButton = rootView.findViewById(R.id.clearButton);
        TextView closeButton = rootView.findViewById(R.id.closeButton);
        TextView startDateDescView = rootView.findViewById(R.id.startDateDescView);
        TextView endDateDescView = rootView.findViewById(R.id.endDateDescView);
        startDateDescView.setText(startDesc + "日期");
        endDateDescView.setText(endDesc + "日期");
        selectDoneButton.setChangeAlphaWhenPress(true);
        selectDoneButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnClickListener != null) {
                    String startDate = mList.get(startGroupPosition)
                            .getList()
                            .get(startChildPosition)
                            .getDate();
                    String endDate = mList.get(endGroupPosition)
                            .getList()
                            .get(endChildPosition)
                            .getDate();
                    mOnClickListener.getDate(startDate, endDate, startGroupPosition, startChildPosition, endGroupPosition, endChildPosition);
                }
                DatePopupWindow.this.dismiss();
            }
        });
        closeButton.setOnClickListener(v -> DatePopupWindow.this.dismiss());
        clearButton.setOnClickListener(v -> {
            initView();//重置当前view
            setDefaultSelect();//选中初始状态值
        });
        LinearLayoutManager manager = new LinearLayoutManager(activity);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mList = new ArrayList<>();
        mDateAdapter = new DateAdapter(mList);
        dateRecyclerView.setLayoutManager(manager);
        dateRecyclerView.setAdapter(mDateAdapter);
        dateRecyclerView.setItemViewCacheSize(200);
        dateRecyclerView.setHasFixedSize(true);
        dateRecyclerView.setNestedScrollingEnabled(false);
        initData();
    }

    private void initData() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);//当前日期转date
        try {
            if (currentDate == null) {
                return;
            }
            mSetDate = dateFormat.parse(currentDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //初始化日期
        Calendar c = Calendar.getInstance();
        c.setTime(mSetDate);
        int firstM = c.get(Calendar.MONTH) + 1;//获取月份 月份是从0开始，需要+1
        int days = c.get(Calendar.DATE);//日期
        int week = c.get(Calendar.DAY_OF_WEEK);//周几
        //获取当前这个月最大天数
        int maxDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
        DateInfo month = new DateInfo();
        List<DayInfo> dayList = new ArrayList<>();
        month.setDate(c.get(Calendar.YEAR) + "年" + firstM + "月");
        //当前月第一天是周几
        int w = CalendarUtil.getWeekNoFormat(c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-01") - 1;
        //根据该月的第一天，星期几，填充上个月的空白日期
        for (int t = 0; t < w; t++) {
            DayInfo dayInfo = new DayInfo();
            dayInfo.setName("");
            dayInfo.setEnable(false);
            dayInfo.setDate("");
            dayList.add(dayInfo);
        }
        //计算当前月的天数
        for (int i = 1; i <= maxDay; i++) {
            DayInfo dayInfo = new DayInfo();
            dayInfo.setName(i + "");
            dayInfo.setDate(c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-" + i);
            int c_year = Integer.parseInt(currentDate.split("-")[0]);
            int c_month = Integer.parseInt(currentDate.split("-")[1]);
            int c_day = Integer.parseInt(currentDate.split("-")[2]);
            if (c_year == c.get(Calendar.YEAR) && c_month == (c.get(Calendar.MONTH) + 1) && c_day == i) {
                childPosition = dayList.size();
            }
            //设置日期是否可选择
            if (i < days) {
                dayInfo.setEnable(false);
            } else {
                dayInfo.setEnable(true);
            }
            dayList.add(dayInfo);
        }
        month.setList(dayList);
        mList.add(month);
        //获取下2个月的数据
        for (int i = 1; i <= 2; i++) {
            //当前月份循环减1
            c.add(Calendar.MONTH, 1);
            DateInfo nextMonth = new DateInfo();
            List<DayInfo> nextDayList = new ArrayList<>();
            int maxDays = c.getActualMaximum(Calendar.DAY_OF_MONTH);
            nextMonth.setDate(c.get(Calendar.YEAR) + "年" + (c.get(Calendar.MONTH) + 1) + "月");
            //周几
            int weeks = CalendarUtil.getWeekNoFormat(c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-01") - 1;
            //根据该月的第一天，星期几，填充上个月的空白日期
            for (int t = 0; t < weeks; t++) {
                DayInfo dayInfo = new DayInfo();
                dayInfo.setName("");
                dayInfo.setEnable(false);
                dayInfo.setDate("");
                nextDayList.add(dayInfo);
            }
            //该月的所有日期
            for (int j = 0; j < maxDays; j++) {
                DayInfo dayInfo = new DayInfo();
                dayInfo.setName((j + 1) + "");
                dayInfo.setDate(c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-" + (j + 1));
                if ((j + 1) > days && i == 2) {
                    dayInfo.setEnable(false);
                } else {
                    dayInfo.setEnable(true);
                }
                nextDayList.add(dayInfo);
            }
            nextMonth.setList(nextDayList);
            mList.add(nextMonth);
        }
        mDateAdapter.updateData();
    }

    private void setInitSelect() {
        if (0 <= this.startGroupPosition && this.startGroupPosition < mList.size() &&
                0 <= this.endGroupPosition && this.endGroupPosition < mList.size()) {
            int maxStartChild = mList.get(this.startGroupPosition).getList().size();
            int maxEndChild = mList.get(this.endGroupPosition).getList().size();
            if (0 <= this.startChildPosition && this.startChildPosition < maxStartChild &&
                    0 <= this.endChildPosition && this.endChildPosition < maxEndChild) {
                setInit();
            } else {
                setDefaultSelect();//设置根据mDate设定今天和昨天日期
            }
        } else {
            setDefaultSelect();//设置根据mDate设定今天和昨天日期
        }
    }

    private void setInit() {
        mList.get(this.startGroupPosition).getList().get(this.startChildPosition).setStatus(1);
        mList.get(this.endGroupPosition).getList().get(this.endChildPosition).setStatus(2);
        mDateAdapter.notifyDataSetChanged();
        getOffsetDate(mList.get(startGroupPosition).getList().get(startChildPosition).getDate(),
                mList.get(endGroupPosition).getList().get(endChildPosition).getDate(), true);
        dateRecyclerView.scrollToPosition(this.startGroupPosition);
    }

    //设置日历标明当前日期的状态
    private void setDefaultSelect() {
        if (childPosition == -1) return;
        String date = mList.get(0).getList().get(childPosition).getDate();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        Date curDate = null;
        try {
            curDate = sdf.parse(formatDate(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (curDate == null) return;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(curDate);
        calendar.add(Calendar.DATE, 1);

        int year = Integer.parseInt(date.split("-")[0]);
        int month = Integer.parseInt(date.split("-")[1]);
        if (year == calendar.get(Calendar.YEAR) && month == calendar.get(Calendar.MONTH) + 1
                && childPosition < mList.get(0).getList().size() - 1) {
            this.startGroupPosition = 0;
            this.startChildPosition = childPosition;
            this.endGroupPosition = 0;
            this.endChildPosition = childPosition - 1;
            setInit();
        } else {
            for (int i = 0; i < mList.get(1).getList().size(); i++) {
                if (!TextUtils.isEmpty(mList.get(1).getList().get(i).getDate())) {
                    this.startGroupPosition = 0;
                    this.startChildPosition = childPosition;
                    this.endGroupPosition = 1;
                    this.endChildPosition = i;
                    setInit();
                    break;
                }
            }
        }
    }

    /**
     * 设置起始时间和结束时间的选中标识，或者设置不选中
     *
     * @param startDate
     * @param endDate
     * @param status    选中设置为true 设置不选中false
     */
    @SuppressLint("SetTextI18n")
    private void getOffsetDate(String startDate, String endDate, boolean status) {
        //更新开始日期和结束日期的信息和状态
        Calendar sCalendar = CalendarUtil.toDate(startDate);
        Calendar eCalendar = CalendarUtil.toDate(endDate);
        startDateView.setText((sCalendar.get(Calendar.MONTH) + 1) + "月" + sCalendar.get(Calendar.DAY_OF_MONTH) + "日");
        startWeekView.setText("周" + CalendarUtil.getWeekByFormat(startDate));
        endDateView.setText((eCalendar.get(Calendar.MONTH) + 1) + "月" + eCalendar.get(Calendar.DAY_OF_MONTH) + "日");
        endWeekView.setText("周" + CalendarUtil.getWeekByFormat(endDate));
        int daysOffset = Integer.parseInt(CalendarUtil.getTwoDay(endDate, startDate));
        if (daysOffset < 0) return;
        if (dayFlag) {
            sumTimeView.setText("共" + (daysOffset + 1) + "天");
        } else {
            sumTimeView.setText("共" + daysOffset + "晚");
        }
        //更改结束日期和完成按钮状态
        endDateLayout.setVisibility(View.VISIBLE);
        hintTextTextView.setVisibility(View.GONE);
        initButton("完成", true);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        Calendar c = Calendar.getInstance();
        DayInfo info = mList.get(startGroupPosition).getList().get(startChildPosition);
        try {
            c.setTime(sdf.parse(info.getDate()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //根据2个时间的相差天数去循环
        for (int i = 0; i < daysOffset; i++) {
            //下一天（目标天）
            c.add(Calendar.DATE, 1);
            //改天的日期（目标天）
            String d = c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-" + c.get(Calendar.DATE);
            //循环group列表
            for (int j = 0; j < mList.size(); j++) {
                //获取该月的随机一个dayInfo
                DayInfo dayInfo = mList.get(j).getList().get(mList.get(j).getList().size() - 1);
                boolean isCheck = false;
                //判断该天是否和目标天是否是同一个月
                if (!TextUtils.isEmpty(dayInfo.getDate())
                        && Integer.parseInt(dayInfo.getDate().split("-")[0]) == (c.get(Calendar.YEAR))
                        && Integer.parseInt(dayInfo.getDate().split("-")[1]) == ((c.get(Calendar.MONTH) + 1))) {
                    //是同一个月，则循环该月多有天数
                    for (int t = 0; t < mList.get(j).getList().size(); t++) {
                        //找到该月的日期与目标日期相同，存在，设置选择标记
                        if (mList.get(j).getList().get(t).getDate().equals(d)) {
                            mList.get(j).getList().get(t).setSelect(status);
                            isCheck = true;
                            break;
                        }
                    }
                }
                if (isCheck) {
                    mDateAdapter.notifyItemChanged(j);
                    break;
                }
            }
        }
    }

    private String formatDate(String date) {
        if (TextUtils.isEmpty(date)) return "";
        return date.split("-")[0] +
                "-" +
                (date.split("-")[1].length() < 2 ? "0" + date.split("-")[1] : date.split("-")[1]) +
                "-" +
                (date.split("-")[2].length() < 2 ? "0" + date.split("-")[2] : date.split("-")[2]);
    }

    private void create(View view) {
        this.showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }

    /**
     * return startDate、endDate(格式：2012-12-10)
     * 选中完成后返回开始时间和结束时间
     * return startGroupPosition、startChildPosition、endGroupPosition、endChildPosition
     * 返回选中时间区间的状态标记，监听中接收后在builder中setInitSelect()方法中直接传出入（可用于记录上次选中的状态，用户再点击进入的时候恢复上一次的区间选中状态）
     */
    public interface DateOnClickListener {
        void getDate(String startDate, String endDate, int startGroupPosition, int startChildPosition, int endGroupPosition, int endChildPosition);
    }

    public static class Builder {
        private String date;
        private Activity activity;
        private View parentView;
        private String startDesc;
        private String endDesc;
        private boolean dayFlag = true;
        private int startGroupPosition = -1;
        private int endGroupPosition = -1;
        private int startChildPosition = -1;
        private int endChildPosition = -1;
        private DateOnClickListener mOnClickListener = null;

        public Builder(Activity activity, Date date, View parentView) {
            this.date = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA).format(date);
            this.activity = activity;
            this.parentView = parentView;
            this.startDesc = "开始";
            this.endDesc = "结束";
            this.dayFlag = true;
        }

        public DatePopupWindow builder() {
            return new DatePopupWindow(this);
        }

        public Builder setInitSelect(int startGroup, int startChild, int endGroup, int endChild) {
            this.startGroupPosition = startGroup;
            this.startChildPosition = startChild;
            this.endGroupPosition = endGroup;
            this.endChildPosition = endChild;
            return this;
        }

        public Builder setInitDay(boolean dayFlag) {
            this.dayFlag = dayFlag;
            if (dayFlag) {
                this.startDesc = "开始";
                this.endDesc = "结束";
            } else {
                this.startDesc = "入住";
                this.endDesc = "离开";
            }
            return this;
        }

        public Builder setDateOnClickListener(DateOnClickListener mlListener) {
            mOnClickListener = mlListener;
            return this;
        }
    }

    private class DateAdapter extends BaseQuickAdapter<DateInfo, BaseViewHolder> {

        DateAdapter(@Nullable List<DateInfo> data) {
            super(R.layout.adapter_select_date, data);
        }

        @Override
        public void onBindViewHolder(BaseViewHolder holder, int positions) {
            super.onBindViewHolder(holder, positions);
            TextView monthTitleView = holder.getView(R.id.monthTitleView);
            monthTitleView.setText(mList.get(positions).getDate());
        }

        @Override
        protected void convert(final BaseViewHolder helper, final DateInfo item) {
            RecyclerView monthRecyclerView = helper.getView(R.id.monthRecyclerView);

            GridLayoutManager manager = new GridLayoutManager(activity, 7);
            monthRecyclerView.setLayoutManager(manager);
            final TempAdapter groupAdapter = new TempAdapter(item.getList());
            monthRecyclerView.setAdapter(groupAdapter);
            monthRecyclerView.setItemViewCacheSize(200);
            monthRecyclerView.setHasFixedSize(true);
            monthRecyclerView.setNestedScrollingEnabled(false);
            groupAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    if (!item.getList().get(position).isEnable()) return;
                    if (TextUtils.isEmpty(item.getList().get(position).getName())) return;
                    if (TextUtils.isEmpty(item.getList().get(position).getDate())) return;
                    int status = item.getList().get(position).getStatus();
                    if (status == 0 &&
                            startGroupPosition == -1 &&
                            startChildPosition == -1 &&
                            item.getList().get(position).isEnable()) {
                        //开始
                        item.getList().get(position).setStatus(1);
                        adapter.notifyItemChanged(position);
                        startGroupPosition = helper.getAdapterPosition();
                        startChildPosition = position;

                        startDateView.setText(CalendarUtil.FormatDateMD(item.getList().get(position).getDate()));
                        startWeekView.setText("周" + CalendarUtil.getWeekByFormat(item.getList().get(position).getDate()));
                        sumTimeView.setText("请选择" + endDesc + "时间");
                        initButton("请选择" + endDesc + "时间", false);
                        endDateLayout.setVisibility(View.GONE);
                        hintTextTextView.setText(endDesc + "日期");
                        hintTextTextView.setVisibility(View.VISIBLE);
                        return;
                    }
                    //结束
                    if (status == 0 && endGroupPosition == -1 && endChildPosition == -1) {
                        int offset = Integer.parseInt(CalendarUtil.getTwoDay(item.getList().get(position).getDate()
                                , mList.get(startGroupPosition).getList().get(startChildPosition).getDate()));
                        //判断该离开日期是否比入住时间还小，是则重新设置入住时间。
                        if (offset < 0) {
                            //刷新上一个开始日期
                            mList.get(startGroupPosition).getList().get(startChildPosition).setStatus(0);
                            mDateAdapter.notifyItemChanged(startGroupPosition);
                            //设置新的入开始日期
                            item.getList().get(position).setStatus(1);
                            startGroupPosition = helper.getAdapterPosition();
                            startChildPosition = position;
                            String mStartTime = CalendarUtil.FormatDateMD(item.getList().get(position).getDate());
                            startDateView.setText(mStartTime);
                            startWeekView.setText("周" + CalendarUtil.getWeekByFormat(item.getList().get(position).getDate()));
                            adapter.notifyItemChanged(position);
                            sumTimeView.setText("请选择" + endDesc + "时间");
                            initButton("请选择" + endDesc + "时间", false);
                            endDateLayout.setVisibility(View.GONE);
                            hintTextTextView.setText(endDesc + "日期");
                            hintTextTextView.setVisibility(View.VISIBLE);
                            return;
                        }
                        //结束
                        item.getList().get(position).setStatus(2);
                        adapter.notifyItemChanged(position);
                        endGroupPosition = helper.getAdapterPosition();
                        endChildPosition = position;
                        getOffsetDate(mList.get(startGroupPosition).getList().get(startChildPosition).getDate(),
                                mList.get(endGroupPosition).getList().get(endChildPosition).getDate(), true);
                        return;
                    }
                    //重置开始和结束时间，设置开始时间
                    if (status == 0 && endGroupPosition != -1 && endChildPosition != -1 && startChildPosition != -1 && startGroupPosition != -1) {
                        //重置开始和结束
                        mList.get(startGroupPosition).getList().get(startChildPosition).setStatus(0);
                        mList.get(endGroupPosition).getList().get(endChildPosition).setStatus(0);
                        mDateAdapter.notifyItemChanged(startGroupPosition);
                        mDateAdapter.notifyItemChanged(endGroupPosition);
                        //重置选择间区的状态
                        getOffsetDate(mList.get(startGroupPosition).getList().get(startChildPosition).getDate(),
                                mList.get(endGroupPosition).getList().get(endChildPosition).getDate(), false);
                        //设置开始
                        item.getList().get(position).setStatus(1);
                        adapter.notifyItemChanged(position);
                        String mStartTime = CalendarUtil.FormatDateMD(item.getList().get(position).getDate());
                        startDateView.setText(mStartTime);
                        startWeekView.setText("周" + CalendarUtil.getWeekByFormat(item.getList().get(position).getDate()));

                        startGroupPosition = helper.getAdapterPosition();
                        startChildPosition = position;
                        endGroupPosition = -1;
                        endChildPosition = -1;
                        sumTimeView.setText("请选择" + endDesc + "时间");
                        initButton("请选择" + endDesc + "时间", false);
                        endDateLayout.setVisibility(View.GONE);
                        hintTextTextView.setText(endDesc + "日期");
                        hintTextTextView.setVisibility(View.VISIBLE);
                    }
                }
            });
        }

        void updateData() {
            notifyDataSetChanged();
        }
    }

    private class TempAdapter extends BaseQuickAdapter<DayInfo, BaseViewHolder> {
        TempAdapter(@Nullable List<DayInfo> data) {
            super(R.layout.adapter_select_date_child, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, DayInfo item) {
            String name = item.getName();
            boolean isSelect = item.isSelect();
            boolean isEnable = item.isEnable();
            int status = item.getStatus();
            helper.setText(R.id.dateView, name);
            //默认
            if (status == 0) {
                if (isSelect) {
                    //选中
                    helper.getView(R.id.dateView).setVisibility(View.VISIBLE);
                    helper.getView(R.id.statusView).setVisibility(View.GONE);
                    helper.getView(R.id.dateDelView).setVisibility(View.GONE);
                    ((TextView) helper.getView(R.id.dateView)).setTextColor(activity.getResources().getColor(R.color.white));
                    (helper.getView(R.id.selectedLayout)).setBackgroundColor(activity.getResources().getColor(R.color.mainThemeColor));
                } else {
                    //没选中状态
                    helper.getView(R.id.dateView).setVisibility(View.VISIBLE);
                    helper.getView(R.id.statusView).setVisibility(View.GONE);
                    helper.getView(R.id.dateDelView).setVisibility(View.GONE);
                    ((TextView) helper.getView(R.id.dateView)).setTextColor(activity.getResources().getColor(R.color.black));
                    (helper.getView(R.id.selectedLayout)).setBackgroundColor(activity.getResources().getColor(R.color.white));
                }
            } else if (status == 1) {
                //开始
                helper.getView(R.id.dateView).setVisibility(View.VISIBLE);
                helper.setText(R.id.statusView, startDesc);
                helper.getView(R.id.statusView).setVisibility(View.VISIBLE);
                helper.getView(R.id.dateDelView).setVisibility(View.GONE);
                ((TextView) helper.getView(R.id.statusView)).setTextColor(activity.getResources().getColor(R.color.white));
                ((TextView) helper.getView(R.id.dateView)).setTextColor(activity.getResources().getColor(R.color.white));
                (helper.getView(R.id.selectedLayout)).setBackgroundColor(activity.getResources().getColor(R.color.mainThemeColor));
            } else if (status == 2) {
                //结束
                helper.getView(R.id.dateView).setVisibility(View.VISIBLE);
                helper.setText(R.id.statusView, endDesc);
                helper.getView(R.id.statusView).setVisibility(View.VISIBLE);
                helper.getView(R.id.dateDelView).setVisibility(View.GONE);
                ((TextView) helper.getView(R.id.statusView)).setTextColor(activity.getResources().getColor(R.color.white));
                ((TextView) helper.getView(R.id.dateView)).setTextColor(activity.getResources().getColor(R.color.white));
                (helper.getView(R.id.selectedLayout)).setBackgroundColor(activity.getResources().getColor(R.color.mainThemeColor));
            }
            //设置当前日期前的样式，没选中，并状态为0情况下
            if (!isSelect && status == 0) {
                if (!isEnable) {
                    //无效
                    TextView textView = helper.getView(R.id.dateDelView);
                    if (TextUtils.isEmpty(name)) {
                        textView.setVisibility(View.GONE);
                    } else {
                        textView.setText(name);
                        textView.setVisibility(View.VISIBLE);
                    }
                    textView.setTextColor(activity.getResources().getColor(R.color.mainThemeColor));
                    helper.getView(R.id.dateView).setVisibility(View.GONE);
                    helper.getView(R.id.statusView).setVisibility(View.GONE);
                } else {
                    helper.getView(R.id.dateView).setVisibility(View.VISIBLE);
                    helper.getView(R.id.statusView).setVisibility(View.GONE);
                    helper.getView(R.id.dateDelView).setVisibility(View.GONE);
                    TextView textView = helper.getView(R.id.dateView);
                    textView.setTextColor(activity.getResources().getColor(R.color.black));
                }
            }
        }
    }

    private class ShareDismissListener implements OnDismissListener {
        @Override
        public void onDismiss() {
            backgroundAlpha(activity);
        }
    }

    private void initButton(String msg, boolean enable) {
        selectDoneButton.setText(msg);
        if (enable) {
            selectDoneButton.setBackgroundColor(ActivityCompat.getColor(activity, R.color.mainThemeColor));
        } else {
            selectDoneButton.setBackgroundColor(ActivityCompat.getColor(activity, R.color.hintColor));
        }
        selectDoneButton.setEnabled(enable);
    }
}
