package com.rv.points;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.frame.base.url.ImageUrl;
import com.frame.rv.config.RvFrameConfig;
import com.ruiwenliu.wrapper.base.BaseBean;
import com.ruiwenliu.wrapper.base.BaseStatusActivity;
import com.ruiwenliu.wrapper.base.presenter.CommonPresenter;
import com.ruiwenliu.wrapper.util.TransformationUtil;
import com.ruiwenliu.wrapper.util.glide.GlideOptions;
import com.ruiwenliu.wrapper.weight.TitleView;
import com.rv.component.utils.DisplayUtil;
import com.rv.home.rv.module.ApiConfig;
import com.rv.points.adapter.DateAdapter;
import com.rv.points.bean.SignRecordBean;
import com.rv.points.bean.SignResultBean;
import com.rv.points.utils.DateUtils;
import com.rv.points.view.InnerGridView;
import com.yuyife.okgo.OkGoUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SignActivity extends BaseStatusActivity<CommonPresenter> {

    @BindView(R2.id.btn_sign)
    Button btnSign;
    @BindView(R2.id.view_month)
    InnerGridView innerGridView;
    @BindView(R2.id.tv_sign_day)
    TextView tvSignDay;
    @BindView(R2.id.img_back)
    ImageView imgBack;
    @BindView(R2.id.iv_sign_top_bg)
    ImageView ivSignTopBg;
    private DateAdapter dateAdapter;

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

    @Override
    protected void initView(Bundle savedInstanceState, TitleView titleView, Intent intent) {

        GlideOptions options = GlideOptions.placeholderOf(com.ruiwenliu.wrapper.R.drawable.glide_icon_placeholder).
                error(com.ruiwenliu.wrapper.R.drawable.glide_icon_error);
        TransformationUtil utils = new TransformationUtil(ivSignTopBg);
        Glide.with(this)
                .asBitmap()
                .load(ImageUrl.IMG_BG_BANNER_SIGN)
                .apply(options)
                .apply(RequestOptions
                        .bitmapTransform(new RoundedCorners(12)).override(ivSignTopBg.getWidth(), ivSignTopBg.getHeight())
                        .disallowHardwareConfig())
                .into(utils);

        titleView.setVisibility(View.GONE);
        btnSign.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sign();
            }
        });
        imgBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        dateAdapter = new DateAdapter(this);
        innerGridView.setAdapter(dateAdapter);
    }

    @Override
    protected void loadData(Bundle savedInstanceState, Intent intent) {
        super.loadData(savedInstanceState, intent);
        getSignList();
    }

    @Override
    public void onShowResult(int requestType, BaseBean result) {
        if (requestType == 0) {
            SignRecordBean bean = (SignRecordBean) result;
            if (bean.getData() != null && bean.getData().size() > 0) {
                SpannableString msp = new SpannableString("累计签到 " + bean.getData().size() + " 天");
                msp.setSpan(new ForegroundColorSpan(Color.parseColor("#FDC349")), 5, msp.length() - 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                msp.setSpan(new AbsoluteSizeSpan(DisplayUtil.sp2px(getApplicationContext(), 18)), 5, msp.length() - 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                tvSignDay.setText(msp);
                dateAdapter.setSignData(bean.getData());
            }
        }
        if (requestType == 1) {
            Toast.makeText(getApplicationContext(), "签到成功", Toast.LENGTH_SHORT).show();
            getSignList();
        }
    }

    private void getSignList() {
        if (!TextUtils.isEmpty(OkGoUtil.getToken())) {
            Map<String, Object> header = new HashMap<>();
            header.put("Authorization", OkGoUtil.getToken());

            Map<String, Object> map = new HashMap<>();
            map.put("startTime", DateUtils.getBeginTime());
            map.put("endTime", DateUtils.getEndTime());
            map.put("integralRuleCode", "SIGN");
            mPresenter.getData(RvFrameConfig.HOST, 0, ApiConfig.HTTP_URL_SIGN_LIST, SignRecordBean.class, map, header, true);
        }
    }

    /*****
     * 签到
     */
    private void sign() {
        if (!TextUtils.isEmpty(OkGoUtil.getToken())) {
            Map<String, Object> header = new HashMap<>();
            header.put("Authorization", OkGoUtil.getToken());

            Map<String, Object> body = new HashMap<>();
            body.put("integralRuleCode", "SIGN");
            mPresenter.postData(RvFrameConfig.HOST, 1, ApiConfig.HTTP_URL_SIGN, SignResultBean.class, body, header, true);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
