package com.rv.plugin.calendar;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.ruiwenliu.wrapper.base.BaseBean;
import com.ruiwenliu.wrapper.base.BaseStatusActivity;
import com.ruiwenliu.wrapper.weight.TitleView;
import com.rv.component.utils.DateUtils;
import com.rv.component.utils.MyUtils;
import com.rv.plugin.calendar.bean.DateInfo;
import com.rv.plugin.calendar.presenter.CalendarPresenter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 日历
 */
public class CalendarActivity extends BaseStatusActivity<CalendarPresenter> {

    @BindView(R2.id.v_calendarView)
    CalendarView2 vCalendarView;
    @BindView(R2.id.tv_calendar_inday)
    TextView tvCalendarInday;
    @BindView(R2.id.tv_calendar_inweek)
    TextView tvCalendarInweek;
    @BindView(R2.id.tv_calendar_intime)
    TextView tvCalendarIntime;
    @BindView(R2.id.tv_calendar_night)
    TextView tvCalendarNight;
    @BindView(R2.id.tv_calendar_outday)
    TextView tvCalendarOutday;
    @BindView(R2.id.tv_calendar_outweek)
    TextView tvCalendarOutweek;
    @BindView(R2.id.tv_calendar_outtime)
    TextView tvCalendarOuttime;
    @BindView(R2.id.tv_calendar_sl_intime)
    TextView tvCalendarSlIntime;
    @BindView(R2.id.ll_selecter_intime)
    LinearLayout llSelecterIntime;
    @BindView(R2.id.ll_selecter_outday)
    LinearLayout llSelecterOutday;
    @BindView(R2.id.tv_selecter_outday)
    TextView tvSelecterOutday;


    private List<DateInfo> listCalendarDay;
    private String begDate, endDate, s_begDate, s_endDate, s_begTime, s_endTime, begDay, begTime, endDay, endTime;
    private Context context;
    /**
     * 最大的日历期限
     */
    private final int MAX_SPAN = 366;
    private int spanIndex = 1;


    private TimePopView inTime;
    private TimePopView2 outTime;

    private ArrayList<String> timeList = new ArrayList<>();

    public static Intent getIntent(Context context) {
        return new Intent(context, CalendarActivity.class);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
//        setMyTitle("选择日期");
//        ibActivityTitleRight.setText("确定");
        context = this;
        Intent intent = this.getIntent();
        begDate = intent.getStringExtra("begDate");
        endDate = intent.getStringExtra("endDate");

        begDay = DateUtils.formatDate16(begDate);
        begTime = DateUtils.formatDate17(begDate);
        endDay = DateUtils.formatDate16(endDate);
        endTime = DateUtils.formatDate17(endDate);
        s_begDate = begDay;
        s_endDate = endDay;
        s_begTime = begTime;
        s_endTime = endTime;
        initTime();
        initView();

        tvCalendarInday.setText(begDay);
        tvCalendarInweek.setText("周" + MyUtils.getWeek(begDay));
        tvCalendarIntime.setText(begTime);
        tvCalendarSlIntime.setText(begTime);

        tvCalendarNight.setText(String.valueOf(DateUtils.compareDateDay(endDay, begDay) +1));
        tvCalendarOutday.setText(DateUtils.formatDate15(endDay));
        tvCalendarOutweek.setText("周" + MyUtils.getWeek(endDay));
        tvCalendarOuttime.setText(endTime);
        tvSelecterOutday.setText(endTime);

        listCalendarDay = initDate();

        vCalendarView.setOnDayClickListener(new CalendarView2.OnDayClickListener() {
            @Override
            public void inClick(DateInfo date) {
                s_begDate = date.getDay();
                tvCalendarInday.setText(DateUtils.formatDate15(s_begDate));
                tvCalendarInweek.setText("周" + MyUtils.getWeek(s_begDate));
                tvCalendarNight.setText("0");

                s_endDate = date.getDay();
                tvCalendarOutday.setText(DateUtils.formatDate15(s_endDate));
                tvCalendarOutweek.setText("周" + MyUtils.getWeek(s_endDate));

                s_endTime = DateUtils.formatDate19(s_begTime);
                tvCalendarOuttime.setText(s_endTime);
                tvSelecterOutday.setText(s_endTime);
            }

            @Override
            public void outClick(DateInfo date) {
                s_endDate = date.getDay();
                tvCalendarOutday.setText(DateUtils.formatDate15(s_endDate));
                tvCalendarOutweek.setText("周" + MyUtils.getWeek(s_endDate));
                tvCalendarNight.setText(String.valueOf(DateUtils.compareDateDay(s_endDate, s_begDate) +1));

                //如果不是同一天   租和还时间是酒店默认的
                if (!s_begDate.equals(s_endDate)) {
//                    s_begTime = getSaveData(MyConstant.checkinTime);
                    tvCalendarIntime.setText(s_begTime);
                    tvCalendarSlIntime.setText(s_begTime);

//                    s_endTime = getSaveData(MyConstant.checkoutTime);
                    tvCalendarOuttime.setText(s_endTime);
                    tvSelecterOutday.setText(s_endTime);
                }
            }
        });

        vCalendarView.setData(listCalendarDay);
    }

    @Override
    protected int setLayout() {
        return R.layout.activity_calendar;
    }

    @Override
    protected void initView(Bundle savedInstanceState, TitleView titleView, Intent intent) {
          titleView.setTitle("选择日期");
        titleView.setText(R.id.tv_title_right, "确定");
        titleView.setChildClickListener(R.id.tv_title_right, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(s_begDate)){
                    showToast("请选择租车日期！");
                    return;
                }
                if (TextUtils.isEmpty(s_endDate)){
                    showToast("请选择还车日期！");
                    return;
                }

                if (TextUtils.isEmpty(s_begTime)){
                    showToast("请选择租车时间！");
                    return;
                }
                if (TextUtils.isEmpty(s_endTime)){
                    showToast("请选择还车时间！");
                    return;
                }
                Intent intent = new Intent();
                intent.putExtra("begDate", s_begDate);
                intent.putExtra("endDate", s_endDate );
                intent.putExtra("begTime",s_begTime);
                intent.putExtra("endTime",s_endTime);
                setResult(200, intent);
                finish();
            }
        });
    }

    private void initView() {
        //租车时间
        inTime = new TimePopView(CalendarActivity.this, timeList, new TimePopView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(String date) {
                s_begTime = date;
                tvCalendarIntime.setText(date);
                tvCalendarSlIntime.setText(date);

                //如果是同一天，离店时间小于入住时间
                if (s_begDate.equals(s_endDate) && DateUtils.formatDate20(s_endTime) <= DateUtils.formatDate20(date)) {
                    s_endTime = DateUtils.formatDate19(s_begTime);
                    tvCalendarOuttime.setText(s_endTime);
                    tvSelecterOutday.setText(s_endTime);
                }
            }
        });
        inTime.setCyclic(true);
        inTime.setCancelable(true);
        inTime.setTitle("租车时间");

        //还车时间
        outTime = new TimePopView2(CalendarActivity.this, timeList, new TimePopView2.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(String date) {
                //如果是同一天，离店时间小于入住时间
                if (s_begDate.equals(s_endDate) && DateUtils.formatDate20(s_begTime) >= DateUtils.formatDate20(date)) {
                    return;
                }
                s_endTime = date;
                tvCalendarOuttime.setText(date);
                tvSelecterOutday.setText(date);
            }
        });
        outTime.setCyclic(true);
        outTime.setCancelable(true);
        outTime.setTitle("还车时间");

        //租车时间选择
        llSelecterIntime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (inTime != null) {
                    for (int i = 0; i < timeList.size(); i++) {
                        if (s_begTime.equals(timeList.get(i))) {
                            inTime.setCurrentItem(i);
                            break;
                        }
                    }
                    inTime.show();
                }
            }
        });

        //还车时间选择
        llSelecterOutday.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (outTime != null) {
                    for (int i = 0; i < timeList.size(); i++) {
                        if (s_endTime.equals(timeList.get(i))) {
                            outTime.setCurrentItem(i);
                            break;
                        }
                    }
                    outTime.show();
                }
            }
        });
    }

    /**
     * 初始化时间数据
     */
    private void initTime() {
        for (int i = 0; i < 24; i++) {
            ;
            timeList.add(String.format("%02d", i) + ":00");
            timeList.add(String.format("%02d", i) + ":30");
        }
    }


    /**
     * 初始化日期
     */
    private List<DateInfo> initDate() {

        List<DateInfo> list = new ArrayList<DateInfo>();
        Date date = new Date();
        int monthIndex = 0;
        while (spanIndex < MAX_SPAN) {
            DateInfo dateInfo = new DateInfo();
            Date curDate = CalendarDateUtil.addDate(date, Calendar.MONTH, monthIndex);
            String yyyy = CalendarDateUtil.formatYear(curDate);
            String mm = CalendarDateUtil.formatMonth(curDate);
            String strMonth = yyyy + "年" + mm + "月";
            dateInfo.setMonth(strMonth);
            dateInfo.setTypeDay(DateInfo.VIEW_TYPE_MONTH_TITLE);
            dateInfo.setCanBeChoose(false);
            list.add(dateInfo);
            //添加当月日期
            setCurMonthDay(curDate, list);
            monthIndex++;
        }
        return list;
    }

    /**
     * 计算出当月的每天信息
     *
     * @param date 当月
     * @return
     */
    private void setCurMonthDay(Date date, List<DateInfo> listDay) {

        String yyyyMM = CalendarDateUtil.formatYearMonth(date);
        // 计算当前月的日期
        Calendar cal = Calendar.getInstance();// 获取日历实例
        cal.setTime(date);
        cal.set(Calendar.DATE, 1);// cal设置当前day为当前月第一天
        int firstWeek = cal.get(Calendar.DAY_OF_WEEK) - 1;// 获取当前月第一天为星期几
        int dayNumInMonth = cal.getActualMaximum(Calendar.DATE);// 获取当前月有多少天

        //月头空格
        addSpaceDay(firstWeek, listDay);

        //正常日期
        for (int j = 1; j <= dayNumInMonth; j++) {

            String strDay = "" + j;
            if (j < 10) {
                strDay = "0" + j;
            }
            String curDay = yyyyMM + "-" + strDay;

            // 农历
            DateInfo day = CalendarDateUtil.getLunar(curDay);
            day.setCanBeChoose(true);
            day.setTypeDay(DateInfo.VIEW_TYPE_DAY);
            day.setDefNongTextColor(context.getResources().getColor(R.color.ri_lhui));
            if (day.isHoliday()) {
                day.setDefNongTextColor(context.getResources().getColor(R.color.ri_dhuang));
            }
            day.setDefNongTextSize(context.getResources().getDimension(R.dimen.sp_12));

            // 如果是初一，则计算前一天是否为除夕
            if (day.getDisplayNongliDate().equals("春节")) {
                for (int k = listDay.size() - 1; k > 0; k++) {
                    DateInfo beforeDay = listDay.get(k);// 前一天
                    if (beforeDay.getTypeDay() == DateInfo.VIEW_TYPE_DAY) {
                        beforeDay.setDisplayNongliDate("除夕");
                        beforeDay.setDefNongTextColor(context.getResources().getColor(R.color.ri_dhuang));
                        break;
                    }
                }
            }

            // 公历
            day.setDefTextColor(context.getResources().getColor(R.color.ri_mlv));
            // 周末字体颜色
            if ((j + firstWeek) % 7 == 0 || (j + firstWeek - 1) % 7 == 0) {
                day.setDefTextColor(context.getResources().getColor(R.color.ri_dhong));
            }
            day.setDefTextSize(context.getResources().getDimension(R.dimen.size_12));
            if (CalendarDateUtil.getCurDate().equals(curDay)) {
                strDay = "今天";
                day.setCurDay(true);
            } else if (CalendarDateUtil.compareDateDay(CalendarDateUtil.getCurDate(), curDay) > 0 || spanIndex >= MAX_SPAN) {
                //如果是今天以前
                day.setDefTextColor(context.getResources().getColor(R.color.ri_lhui));
                day.setDefNongTextColor(context.getResources().getColor(R.color.ri_lhui));
                day.setCanBeChoose(false);
            } else {
                spanIndex++;
            }

            if (curDay.equals(begDay)) {
                day.setBeginDay(true);
            }
            if (curDay.equals(endDay)) {
                day.setEndDay(true);
            }
            day.setDay(curDay);
            day.setDisplayDay(strDay);
            day.setBgColor(Color.WHITE);

            listDay.add(day);

        }

        //月尾空格
        int endWeek = 6 - cal.get(Calendar.DAY_OF_WEEK) - 1;// 获取当前月最后一天星期几
        addSpaceDay(endWeek, listDay);
    }

    /**
     * 添加空格日期
     *
     * @param daySize 空格日期数
     * @param listDay 日期列表
     */
    private void addSpaceDay(int daySize, List<DateInfo> listDay) {
        for (int i = 0; i < daySize; i++) {
            DateInfo daySpace = new DateInfo();
            daySpace.setDay("");
            daySpace.setDisplayDay("");
            daySpace.setNongliDate("");
            daySpace.setDisplayNongliDate("");
            daySpace.setTypeDay(DateInfo.VIEW_TYPE_SPACE);
            daySpace.setCanBeChoose(false);
            listDay.add(daySpace);
        }
    }


    @Override
    public void onShowResult(int requestType, BaseBean result) {

    }
}
