package com.rehome.meetingbook.ui.activity;

import androidx.appcompat.widget.Toolbar;

import android.graphics.Color;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;

import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarView;
import com.rehome.meetingbook.Contans;
import com.rehome.meetingbook.R;
import com.rehome.meetingbook.adapter.AppointmentDateAdapter;
import com.rehome.meetingbook.adapter.YunQingExalAdapter;
import com.rehome.meetingbook.base.BaseActivity2;
import com.rehome.meetingbook.bean.AppointmentDateDetailListBean;
import com.rehome.meetingbook.bean.AppointmentDateListBean;
import com.rehome.meetingbook.bean.AppointmentLevelBean;
import com.rehome.meetingbook.bean.YunQingExalBean;
import com.rehome.meetingbook.databinding.ActivitySimpleBinding;
import com.rehome.meetingbook.utils.GsonUtils;
import com.rehome.meetingbook.utils.HttpListener;
import com.rehome.meetingbook.utils.NoProgresshttpUtils;
import com.rehome.meetingbook.utils.StatusBarUtil;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SimpleActivity extends BaseActivity2<ActivitySimpleBinding>implements
        CalendarView.OnCalendarSelectListener,
        CalendarView.OnYearChangeListener{

    private int mYear;

    //培训考试模式
    private List<AppointmentLevelBean.RowsBean> appointmentExamModel;
    //日常培训考试
    private List<AppointmentDateDetailListBean.RowsBean> daylyExamList;
    private AppointmentDateAdapter daylyAdapter;
    //运行部考试
    private List<YunQingExalBean.RowsBean> yqExalList;
    private YunQingExalAdapter yqExalAdapter;

    @Override
    protected ActivitySimpleBinding getBinding() {
        return ActivitySimpleBinding.inflate(LayoutInflater.from(this));
    }

    @Override
    protected Toolbar getToolbar() {
        return binding.toolbarView.toolbar;
    }

    @Override
    protected void initView() {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        StatusBarUtil.setColor(SimpleActivity.this, Color.parseColor("#138CF2"));
        binding.toolbarView.title.setText("预约一览");
        setExitZx();

        appointmentExamModel = new ArrayList<>();
        daylyExamList = new ArrayList<>();
        yqExalList = new ArrayList<>();
        daylyAdapter = new AppointmentDateAdapter(SimpleActivity.this,daylyExamList);
        yqExalAdapter = new YunQingExalAdapter(SimpleActivity.this,yqExalList);
        binding.lvAppointmentDayly.setAdapter(daylyAdapter);
        binding.lvAppointmentYy.setAdapter(yqExalAdapter);


        mYear = binding.calendarView.getCurYear();
        binding.tvYear.setText(String.valueOf(binding.calendarView.getCurYear()));
        binding.flCurrent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.calendarView.scrollToCurrent();
            }
        });

        binding.tvMonthDay.setText(binding.calendarView.getCurMonth() + "月" + binding.calendarView.getCurDay() + "日");
        binding.tvLunar.setText("今日");
        binding.tvCurrentDay.setText(String.valueOf(binding.calendarView.getCurDay()));

        binding.calendarView.setOnYearChangeListener(this);
        binding.calendarView.setOnCalendarSelectListener(this);
    }

    @Override
    protected void initData() {

//        int year = binding.calendarView.getCurYear();
//        int month = binding.calendarView.getCurMonth();
//
//        Map<String, Calendar> map = new HashMap<>();
//        map.put(getSchemeCalendar(year, month, 3, 0xFF40db25, "假").toString(),
//                getSchemeCalendar(year, month, 3, 0xFF40db25, "假"));
//        map.put(getSchemeCalendar(year, month, 6, 0xFF40db25, "事").toString(),
//                getSchemeCalendar(year, month, 6, 0xFF40db25, "事"));
//        map.put(getSchemeCalendar(year, month, 9, 0xFF40db25, "议").toString(),
//                getSchemeCalendar(year, month, 9, 0xFF40db25, "议"));
//        map.put(getSchemeCalendar(year, month, 13, 0xFF40db25, "记").toString(),
//                getSchemeCalendar(year, month, 13, 0xFF40db25, "记"));
//        map.put(getSchemeCalendar(year, month, 14, 0xFF40db25, "记").toString(),
//                getSchemeCalendar(year, month, 14, 0xFF40db25, "记"));
//        map.put(getSchemeCalendar(year, month, 15, 0xFF40db25, "假").toString(),
//                getSchemeCalendar(year, month, 15, 0xFF40db25, "假"));
//        map.put(getSchemeCalendar(year, month, 18, 0xFF40db25, "记").toString(),
//                getSchemeCalendar(year, month, 18, 0xFF40db25, "记"));
//        map.put(getSchemeCalendar(year, month, 25, 0xFF40db25, "假").toString(),
//                getSchemeCalendar(year, month, 25, 0xFF40db25, "假"));
//        map.put(getSchemeCalendar(year, month, 27, 0xFF40db25, "多").toString(),
//                getSchemeCalendar(year, month, 27, 0xFF40db25, "多"));
//        //此方法在巨大的数据量上不影响遍历性能，推荐使用
//        binding.calendarView.setSchemeDate(map);

        //获取培训考试模式
        getExamModel();
    }

    private Calendar getSchemeCalendar(int year, int month, int day, int color, String text) {
        Calendar calendar = new Calendar();
        calendar.setYear(year);
        calendar.setMonth(month);
        calendar.setDay(day);
        calendar.setSchemeColor(color);//如果单独标记颜色、则会使用这个颜色
        calendar.setScheme(text);
        return calendar;
    }

    private void setExitZx() {
        mToolbar.setNavigationIcon(R.mipmap.back);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void onCalendarOutOfRange(Calendar calendar) {

    }

    @Override
    public void onCalendarSelect(Calendar calendar, boolean isClick) {
        binding.tvLunar.setVisibility(View.VISIBLE);
        binding.tvYear.setVisibility(View.VISIBLE);
        binding.tvMonthDay.setText(calendar.getMonth() + "月" + calendar.getDay() + "日");
        binding.tvYear.setText(String.valueOf(calendar.getYear()));
        binding.tvLunar.setText(calendar.getLunar());
        mYear = calendar.getYear();
        Log.i("app","onCalendarSelect");
        Log.i("app",String.valueOf(calendar.getYear())+"-"+String.valueOf(calendar.getMonth())+"-"+calendar.getDay());

        getDateDetailList(String.valueOf(calendar.getYear())+"-"+String.format("%02d", calendar.getMonth())+"-"+String.format("%02d", calendar.getDay()));
        //获取生产岗位培训室使用时间
        getScgwDateDetailList(String.valueOf(calendar.getYear())+"-"+String.format("%02d", calendar.getMonth())+"-"+String.format("%02d", calendar.getDay()));


    }

    @Override
    public void onYearChange(int year) {
        binding.tvMonthDay.setText(String.valueOf(year));
    }

    private void getExamModel() {

        String url = Contans.BASE_URL + Contans.GET_YYTYPEANDEXAMMODEL_URL + "PSKSMS";
        Log.i("app", url);
        Request<String> request = NoHttp.createStringRequest(url, RequestMethod.GET);

        NoProgresshttpUtils.getInstance().add(0, request, new HttpListener<String>() {

            @Override
            public void onSucceed(int what, Response<String> response) {

                Log.i("app", "----------------");
                String result = response.get();
                Log.i("app", result);

                AppointmentLevelBean bean = GsonUtils.GsonToBean(result, AppointmentLevelBean.class);
                if (bean != null && bean.getRows() != null && bean.getRows().size() > 0) {
                    appointmentExamModel.clear();
                    appointmentExamModel.addAll(bean.getRows());
                    //获取有预约的日期（从当前时间起）
                    getDateList();
                }
            }

            @Override
            public void onFailed(int what, Response<String> response) {

            }
        });
    }
    //获取有预约的日期（从当前时间起）
    private void getDateList() {

        String url = Contans.BASE_URL + Contans.GET_YYDATE_URL;
        Log.i("app", url);
        Request<String> request = NoHttp.createStringRequest(url, RequestMethod.GET);

        NoProgresshttpUtils.getInstance().add(0, request, new HttpListener<String>() {

            @Override
            public void onSucceed(int what, Response<String> response) {

                Log.i("app", "----------------");
                String result = response.get();
                Log.i("app", result);

                AppointmentDateListBean bean = GsonUtils.GsonToBean(result, AppointmentDateListBean.class);
                if (bean != null) {
                    if (bean.getRows() != null && bean.getRows().size() > 0) {
                        Map<String, Calendar> map = new HashMap<>();
                        for (AppointmentDateListBean.RowsBean item:bean.getRows()) {
                            if(!TextUtils.isEmpty(item.getYyrq())){
                                String year = item.getYyrq().substring(0,4);
                                String month = item.getYyrq().substring(5,7);
                                String day = item.getYyrq().substring(8,10);
                                map.put(getSchemeCalendar(Integer.parseInt(year),Integer.parseInt(month),Integer.parseInt(day),0xFF40db25,"假").toString(),
                                        getSchemeCalendar(Integer.parseInt(year),Integer.parseInt(month),Integer.parseInt(day),0xFF40db25,"假"));
                            }
                        }
                        //此方法在巨大的数据量上不影响遍历性能，推荐使用
                        binding.calendarView.setSchemeDate(map);
//                        //获取每天预约详细
//                        getDateDetailList(bean.getRows().get(0).getYyrq());
//                        //获取生产岗位培训室使用时间
//                        getScgwDateDetailList(bean.getRows().get(0).getYyrq());

                        binding.calendarView.scrollToCurrent();
                    }
                } else {
                    showToast("预约数据");
                }
            }

            @Override
            public void onFailed(int what, Response<String> response) {

            }
        });
    }

    //获取每天预约详细
    private void getDateDetailList(String dateStr) {

        binding.tvTimeDay1.setText(dateStr+":");
        binding.tvTimeDay3.setText(dateStr+":");
        String url = Contans.BASE_URL + Contans.GET_YYDATEDETAILLIST_URL+dateStr;
        Log.i("app", url);
        Request<String> request = NoHttp.createStringRequest(url, RequestMethod.GET);

        NoProgresshttpUtils.getInstance().add(0, request, new HttpListener<String>() {

            @Override
            public void onSucceed(int what, Response<String> response) {

                Log.i("app", "----------------");
                String result = response.get();
                Log.i("app", result);

                AppointmentDateDetailListBean bean = GsonUtils.GsonToBean(result, AppointmentDateDetailListBean.class);
                if (bean != null) {
                    if (bean.getRows() != null && bean.getRows().size() > 0) {
                        if(appointmentExamModel.size()>0){
                            String idDayly = "";
                            daylyExamList.clear();
                            for (AppointmentLevelBean.RowsBean modelItem:appointmentExamModel) {
                                if(modelItem.getText().equals("日常培训考试")){
                                    idDayly = modelItem.getId();
                                }
                            }
                            for (AppointmentDateDetailListBean.RowsBean item:bean.getRows()) {
                                if(item.getPsmodel().equals(idDayly)){
                                    daylyExamList.add(item);
                                }
                            }
                            daylyAdapter.notifyDataSetChanged();
                        }
                    }else{
                        daylyExamList.clear();
                        daylyAdapter.notifyDataSetChanged();
                    }
                }
            }

            @Override
            public void onFailed(int what, Response<String> response) {

            }
        });
    }
    //获取每天预约详细
    private void getScgwDateDetailList(String dateStr) {

        String url = Contans.BASE_URL + Contans.GET_YYDATESCGW_URL+dateStr;
        Log.i("app", url);
        Request<String> request = NoHttp.createStringRequest(url, RequestMethod.GET);

        NoProgresshttpUtils.getInstance().add(0, request, new HttpListener<String>() {

            @Override
            public void onSucceed(int what, Response<String> response) {

                Log.i("app", "----------------");
                String result = response.get();
                Log.i("app", result);

                YunQingExalBean bean = GsonUtils.GsonToBean(result, YunQingExalBean.class);
                if (bean != null) {
                    if (bean.getRows() != null && bean.getRows().size() > 0) {
                        yqExalList.clear();
                        yqExalList.addAll(bean.getRows());
                        yqExalAdapter.notifyDataSetChanged();
                    }else{
                        yqExalList.clear();
                        yqExalAdapter.notifyDataSetChanged();
                    }
                }
            }

            @Override
            public void onFailed(int what, Response<String> response) {

            }
        });
    }
}