package com.sheep.gamegroup.view.activity;

import android.app.Activity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.view.View;
import android.widget.TextView;

import com.sheep.gamegroup.absBase.BaseActivity;
import com.sheep.gamegroup.di.components.DaggerSignRecordComponent;
import com.sheep.gamegroup.di.modules.SignRecordModule;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.PunchLogEntity;
import com.sheep.gamegroup.model.entity.StatisticsEntity;
import com.sheep.gamegroup.presenter.SignRecordContract;
import com.sheep.gamegroup.presenter.SignRecordPresenter;
import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.gamegroup.util.Jump2View;
import com.sheep.gamegroup.util.ListUtil;
import com.sheep.gamegroup.util.MathUtil;
import com.sheep.gamegroup.view.adapter.expandadapter.RecyclerViewData;
import com.sheep.gamegroup.view.adapter.expandadapter.SignRecordAdapter;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.G;
import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * 我的战绩
 */

public class SignRecordAct extends BaseActivity implements SignRecordContract.View {

    Activity activity;
    @Inject
    SignRecordPresenter presenter;
    @BindView(R.id.txt_baseactivity_title)
    TextView txtBaseactivityTitle;
    @BindView(R.id.sign_tv_record)
    TextView signTvRecord;
    @BindView(R.id.sign_tv_ljtr)
    TextView signTvLjtr;
    @BindView(R.id.sign_tv_ljjj)
    TextView signTvLjjj;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;

    private List<RecyclerViewData> mDatas;
    private SignRecordAdapter adapter;
    View empty_view;

    int index = 0;
    Calendar calendar;

    @Override
    protected int getLayoutId() {
        return R.layout.sign_record_layout;
    }

    @Override
    public void initView() {
        activity = this;
        empty_view = findViewById(R.id.empty_view);
        TitleBarUtils
                .getInstance()
                .setTitle(activity, "我的战绩")
                .setTitleFinish(activity)
                .setRightImgBotton(activity, R.drawable.sign_right_record_bg, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Jump2View.getInstance().goSignRankingsAct(activity, null);
            }
        });
        DaggerSignRecordComponent.builder()
                .netComponent(SheepApp.getInstance().getNetComponent())
                .signRecordModule(new SignRecordModule(this))
                .build()
                .inject(this);
        mDatas = new ArrayList<>();
        recyclerview.setLayoutManager(new LinearLayoutManager(activity));
        calendar = Calendar.getInstance();
        presenter.getStatistics();
        presenter.getPunchLog(1);//获取当月
        presenter.getPunchLog(2);//获取上月
        presenter.getPunchLog(3);//获取上上月
    }


    @Override
    public void initListener() {

    }

    @Override
    public void initData() {
    }


    @Override
    public void succStatistics(BaseMessage o) {
//        {"total_money":1,"continue_punch_count":1,"total_award":2.8,"total_punch_count":1}
        StatisticsEntity statisticsEntity = o.getData(StatisticsEntity.class);
        signTvRecord.setText(String.format(getString(R.string.sign_tv_srecord_money),statisticsEntity.getTotal_punch_count(),statisticsEntity.getContinue_punch_count()));
        signTvLjtr.setText(getSpannableString(MathUtil.twoNumber(statisticsEntity.getTotal_money())));
        signTvLjjj.setText(getSpannableString(MathUtil.twoNumber(statisticsEntity.getTotal_award())));
    }

    /**
     * 打卡记录返回
     * @param o
     */
    @Override
    public void succPunchLog(BaseMessage o,int action) {
        List<PunchLogEntity> list = o.getDatas(PunchLogEntity.class);
        if(!ListUtil.isEmpty(list)){
            int month = calendar.get(Calendar.MONTH) + 1;
            if(action == 1){
                mDatas.add(new RecyclerViewData(month + "月", list, true));
            }else if(action == 2){
                mDatas.add(new RecyclerViewData((month - 1) + "月", list, false));
            }else if(action == 3){
                mDatas.add(new RecyclerViewData((month - 2) + "月", list, false));
            }
        }

        index ++;
        if(index == 3){

            if(ListUtil.isEmpty(mDatas)){
                CommonUtil.getInstance().updateEmptyView(empty_view, true);
                recyclerview.setVisibility(View.GONE);
            }else{
                empty_view.setVisibility(View.GONE);
                recyclerview.setVisibility(View.VISIBLE);
                adapter = new SignRecordAdapter(activity, mDatas);
                recyclerview.setAdapter(adapter);
            }
        }
    }

    @Override
    public void failStatistics(BaseMessage o) {
        hideProgress();
        G.showToast(o.getMsg());
    }

    @Override
    public void failPunchLog(BaseMessage o) {
        hideProgress();
        G.showToast(o.getMsg());
    }


    private SpannableString getSpannableString(String price) {
        String text = String.format(getString(R.string.x_yuan),price);
        SpannableString spannableString = new SpannableString(text);
        spannableString.setSpan(new AbsoluteSizeSpan(14, true), text.length() - 1, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

}
