package com.sheep.gamegroup.view.fragment;

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarView;
import com.kfzs.appstore.utils.adapter.recyclerview.RecyclerViewAdapter;
import com.kfzs.appstore.utils.adapter.recyclerview.ViewHolder;
import com.sheep.gamegroup.model.api.ApiService;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.PunchLog;
import com.sheep.gamegroup.model.entity.StatisticsEntity;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.gamegroup.util.ListUtil;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.gamegroup.view.customview.CalendarMonthView;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.G;

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

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by realicing on 2018/9/17.
 * realicing@sina.com
 */
public class FgtSignCardRecord extends BaseListFragment2<PunchLog> {
    CalendarView sign_card_record_cv;
    TextView total_award;
    TextView total_money;
    TextView total_punch_count;
    TextView continue_punch_count;
    @Override
    public void initView() {
        //添加 CalendarMonthView 的引用，防止无引用，导致误删该类
        CalendarMonthView.class.getSimpleName();
        per_page = 100;
        super.initView();
        view_list.setNestedScrollingEnabled(false);
        View view = LayoutInflater.from(activity).inflate(R.layout.header_sign_card_record, null);
        view_list.addHeaderView(view);
        sign_card_record_cv = view.findViewById(R.id.sign_card_record_cv);
        total_award = view.findViewById(R.id.total_award);
        total_money = view.findViewById(R.id.total_money);
        total_punch_count = view.findViewById(R.id.total_punch_count);
        continue_punch_count = view.findViewById(R.id.continue_punch_count);
        ViewUtil.setDefaultText(total_award);
        ViewUtil.setText(total_money);
        ViewUtil.setText(total_punch_count);
        ViewUtil.setText(continue_punch_count);
    }

    @Override
    public synchronized void initData() {
        super.initData();
        initStatistics();
    }

    @Override
    public void initListener() {
        super.initListener();
        sign_card_record_cv.setOnMonthChangeListener(new CalendarView.OnMonthChangeListener() {
            @Override
            public void onMonthChange(int year, int month) {
                if(isNoMore()){
                    return;
                }
                loadMoreData();
            }
        });
    }

    @Override
    protected RecyclerView.Adapter getAdapter() {
        return new RecyclerViewAdapter<PunchLog>(SheepApp.getInstance(), R.layout.item_sign_card_record, list) {
            @Override
            public void convert(ViewHolder viewHolder, PunchLog item, int position) {
                TextView sign_card_record_date = (TextView) viewHolder.itemView.findViewById(R.id.sign_card_record_date);
                TextView sign_card_record_time = (TextView) viewHolder.itemView.findViewById(R.id.sign_card_record_time);
                TextView sign_card_record_bonus = (TextView) viewHolder.itemView.findViewById(R.id.sign_card_record_bonus);
                ViewUtil.setTextTime(sign_card_record_date, item.getCreate_time(), "yyyy/MM/dd");
                ViewUtil.setTextTime(sign_card_record_time, item.getCreate_time(), "HH:mm");
                ViewUtil.setText(sign_card_record_bonus, item.getAmount() + "元");
            }
        };
    }

    @Override
    protected String getKey(int page, int per_page) {
        return String.format(Locale.CHINA, "app/punch/all_punch_log?page=%d&per_page=%d", page, per_page);
    }

    @Override
    protected Observable<BaseMessage> getApi(ApiService apiService) {
        return apiService.getAllPunchLog(page, per_page);
    }

    @Override
    protected Class<PunchLog> getTClass() {
        return PunchLog.class;
    }

    @Override
    public void notifyData() {
        updateCalendarSchemeDate(newList);
    }
    private void initStatistics() {
        SheepApp.getInstance().getNetComponent().getApiService().getStatistics()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        StatisticsEntity result = baseMessage.getData(StatisticsEntity.class);
                        ViewUtil.setText(total_award, result.getTotal_award());
                        ViewUtil.setText(total_money, result.getTotal_money());
                        ViewUtil.setText(total_punch_count, result.getTotal_punch_count());
                        ViewUtil.setText(continue_punch_count, result.getContinue_punch_count());
                    }

                    @Override
                    public void onError(BaseMessage baseMessage) {
                        G.showToast(baseMessage);
                    }
                });
    }
    /**
     * 标记日期时间
     */
    public void updateCalendarSchemeDate(List<PunchLog> newList) {
        if(ListUtil.isEmpty(newList)){
            return;
        }
        //只加载当年数据
        int year = sign_card_record_cv.getCurYear();
        for (PunchLog item : newList) {
            if(timeMap.contains(item.getCreate_time())){
                continue;
            }
            timeMap.add(item.getCreate_time());
            java.util.Calendar date = java.util.Calendar.getInstance();
            date.setTimeInMillis(item.getCreate_time() * 1000);
            int month = 1 + date.get(java.util.Calendar.MONTH);
            int day = date.get(java.util.Calendar.DAY_OF_MONTH);
            Calendar calendar = getSchemeCalendar(year, month, day);
            map.put(calendar.toString(), calendar);
        }
        sign_card_record_cv.setSchemeDate(map);
    }
    private Map<String, Calendar> map = new HashMap<>();
    private List<Long> timeMap = ListUtil.emptyList();

    private Calendar getSchemeCalendar(int year, int month, int day) {
        Calendar calendar = new Calendar();
        calendar.setYear(year);
        calendar.setMonth(month);
        calendar.setDay(day);
        calendar.setSchemeColor(Color.parseColor("#FD2D54"));
        return calendar;
    }
}
