package com.tofly.ghrq_route.common.ui.activity;

import android.os.Bundle;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;

import com.mgq.base.utils.progressdialog.ProgressManager;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.base.ui.BaseUi;
import com.tofly.ghrq_route.common.ui.view.BaseCalendar;
import com.tofly.ghrq_route.common.ui.view.MoreSelectBlock;
import com.tofly.ghrq_route.module.sign.SignTool;
import com.tofly.ghrq_route.module.sign.controller.SignCallBack;
import com.tofly.ghrq_route.module.sign.domain.SignAttendedDataBean;
import com.tofly.ghrq_route.common.util.date.Dd1;

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;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by zombiexq on 2016/7/14.
 */
public class UiDailyPlan extends BaseUi {
    @BindView(R.id.main_toolbar)
    Toolbar mainToolbar;
    @BindView(R.id.view_calendar)
    BaseCalendar viewCalendar;
    @BindView(R.id.text_date_sign_on)
    TextView textsignOn;
    @BindView(R.id.text_date_unsign_on)
    TextView textUnsignOn;

    private static final String TAG = UiDailyPlan.class.getName();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.ui_daily_plan);
        ButterKnife.bind(this);

        initView();
        initData();
        initListener();
    }

    private void initView() {
        viewCalendar.setBaseBlock(new MoreSelectBlock());
    }

    private void initData() {
        //String date = DateUtils.getCurrentYearMonth();
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        String date = String.format(Locale.CHINA, "%04d-%02d", year, month);
        getDailyAttended(date);
    }

    private void initListener() {
        viewCalendar.setOnDateChangedListener((year, month) -> {
            String date = String.format(Locale.CHINA, "%04d-%02d", year, month);
            getDailyAttended(date);
        });
    }

    private void getDailyAttended(String date){
        if (date != null && !date.isEmpty()) {
            String strUploading = getString(R.string.updating_data);
            ProgressManager.getInstance().showProgress(this, null, strUploading, true);
            SignTool signTool = SignTool.getInstance();
            signTool.getDailyAttended(this, date, new SignCallBack(){
                @Override
                public void onGetDailyPlanAttened(boolean isSuccess, int errorCode, String errMsg, List<SignAttendedDataBean> attendedList) {
                    super.onGetDailyPlanAttened(isSuccess, errorCode, errMsg, attendedList);
                    ProgressManager.getInstance().cancelProgress();
                    int daySignsOn = 0;
                    int dayUnsignsOn = 0;
                    if (isSuccess){
                        if (attendedList != null && attendedList.size() > 0){
                            ArrayList<Dd1> seletDateList = new ArrayList<>();
                            for (SignAttendedDataBean dataBean :  attendedList){
                                Dd1 dd1 = getDateFromAttendedData(dataBean);
                                if (dd1 != null){
                                    seletDateList.add(dd1);
                                    if (dd1.status == 1 || dd1.status == 3)
                                        daySignsOn++;
                                    else
                                        dayUnsignsOn++;
                                }
                            }
                            MoreSelectBlock moreSelectBlock = new MoreSelectBlock(seletDateList);
                            viewCalendar.setBaseBlock(moreSelectBlock);
                        }
                    }
                    textsignOn.setText(daySignsOn + "天");
                    textUnsignOn.setText(dayUnsignsOn + "天");
                }
            });
        }
    }

    private Dd1 getDateFromAttendedData(SignAttendedDataBean dataBean){
        Dd1 dd1 = null;
        if (dataBean != null){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String strDate = dataBean.getTheTime();
            try {
                Date date = sdf.parse(strDate);
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                int year = calendar.get(Calendar.YEAR);
                int month = calendar.get(Calendar.MONTH);
                int day = calendar.get(Calendar.DAY_OF_MONTH);
                dd1 = new Dd1();
                dd1.y = year;
                dd1.m = month;
                dd1.d = day;
                int onTime = dataBean.getOnTime();
                int offTime = dataBean.getOffTime();
                int status = onTime + (offTime << 1);
                dd1.status = status;
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return dd1;
    }
}
