package com.rv.points;

import android.content.Intent;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
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.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.base.utils.ui.image.round.RoundImageView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.ruiwenliu.wrapper.base.BaseBean;
import com.ruiwenliu.wrapper.base.BaseStatusActivity;
import com.ruiwenliu.wrapper.bean.BeanUserInfo;
import com.ruiwenliu.wrapper.bean.UserInfoBean;
import com.ruiwenliu.wrapper.util.glide.GlideManager;
import com.ruiwenliu.wrapper.weight.TitleView;
import com.rv.component.utils.Cache;
import com.rv.component.utils.CacheEnum;
import com.rv.component.utils.DisplayUtil;
import com.rv.home.rv.module.ApiConfig;
import com.rv.home.rv.module.basic.presenter.CommonPresenter;
import com.rv.home.rv.module.ui.main.home.order.bean.OrderListBean;
import com.rv.points.adapter.PointAdapter;
import com.rv.points.bean.PointBean;
import com.rv.points.bean.RulerBean;
import com.rv.points.bean.UserPointInfoBean;
import com.rv.points.presenter.PointPresenter;
import com.yuyife.okgo.OkGoUtil;

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

import butterknife.BindView;

public class PointsActivity extends BaseStatusActivity<PointPresenter> {

    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.rimg_avatar)
    RoundImageView imageView;
    @BindView(R2.id.tv_name)
    TextView tvName;
    @BindView(R2.id.tv_point)
    TextView tvPoint;
    @BindView(R2.id.img_back)
    ImageView imgBack;
    private PointAdapter adapter;
    private UserInfoBean userInfo;

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

    @Override
    protected void initView(Bundle savedInstanceState, TitleView titleView, Intent intent) {
        titleView.setVisibility(View.GONE);
        imgBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        userInfo = (UserInfoBean) Cache.getInstance(getApplicationContext()).get(CacheEnum.USER);
        if (userInfo != null && userInfo.getData() != null) {
            if (!TextUtils.isEmpty(userInfo.getData().getHeadimgurl())) {
                GlideManager.getInstance(getApplicationContext()).loadImage(userInfo.getData().getHeadimgurl(), imageView);
            }

            if (!TextUtils.isEmpty(userInfo.getData().getNickname())) {
                tvName.setText(userInfo.getData().getNickname());
            }
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayout.VERTICAL, false));
        adapter = new PointAdapter();
        recyclerView.setAdapter(adapter);
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                int id = view.getId();
                PointBean.RvPointItem item = (PointBean.RvPointItem) adapter.getItem(position);
                if (R.id.btn_state == id) {
                    if (!TextUtils.isEmpty(item.getCode()))
                        mPresenter.go(item.getCode().toLowerCase().trim());
                } else if (R.id.tv_regulation == id) {
                    getSignRuler();
                }

            }
        });
    }


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

    private void getUserPointInfo() {
        String result = "{\n" +
                "    \"status\": 200,\n" +
                "    \"message\": \"Success\",\n" +
                "    \"data\": {\n" +
                "        \"userId\": 51,\n" +
                "        \"totalPoint\": 0,\n" +
                "        \"restPoint\": 0,\n" +
                "        \"crtTime\": 1563244090568,\n" +
                "        \"updTime\": 1563244090568,\n" +
                "        \"isdel\": false\n" +
                "    },\n" +
                "    \"rel\": true\n" +
                "}";
        UserPointInfoBean infoBean = new Gson().fromJson(result, UserPointInfoBean.class);
        onShowResult(0, infoBean);
    }

//    /********
//     * 获取用户积分信息
//     */
//    private void getUserPointInfo() {
//        if (!TextUtils.isEmpty(OkGoUtil.getToken())) {
//            Map<String, Object> header = new HashMap<>();
//            mPresenter.getData(0, ApiConfig.HTTP_URR_USER_POINT, null, header, true);
//        }
//    }

    private void getPointList() {
        String result = "{\n" +
                "    \"status\": 200,\n" +
                "    \"message\": \"Success\",\n" +
                "    \"data\": {\n" +
                "        \"totalCount\": 13,\n" +
                "        \"totalPage\": 2,\n" +
                "        \"pageNum\": 1,\n" +
                "        \"pageSize\": 10,\n" +
                "        \"data\": [\n" +
                "            {\n" +
                "                \"id\": 43,\n" +
                "                \"period\": 0,\n" +
                "                \"number\": 100,\n" +
                "                \"point\": 0,\n" +
                "                \"price\": 0,\n" +
                "                \"isContinuity\": false,\n" +
                "                \"finishDay\": 0,\n" +
                "                \"finishPoint\": 0,\n" +
                "                \"status\": true,\n" +
                "                \"crtTime\": 1563269072536,\n" +
                "                \"isdel\": false,\n" +
                "                \"startTime\": 0,\n" +
                "                \"endTime\": 0,\n" +
                "                \"updTime\": 1563269072536,\n" +
                "                \"img\": \"\",\n" +
                "                \"orderId\": 11,\n" +
                "                \"name\": \"购买路线\",\n" +
                "                \"code\": \"BUYROUT\",\n" +
                "                \"regulation\": \"购买路线*1\",\n" +
                "                \"otherRule\": \"{\\\"rule\\\":1‘}\",\n" +
                "                \"integralStatus\": false\n" +
                "            },\n" +
                "            {\n" +
                "                \"id\": 42,\n" +
                "                \"period\": 0,\n" +
                "                \"number\": 100,\n" +
                "                \"point\": 0,\n" +
                "                \"price\": 0,\n" +
                "                \"isContinuity\": false,\n" +
                "                \"finishDay\": 0,\n" +
                "                \"finishPoint\": 0,\n" +
                "                \"status\": true,\n" +
                "                \"crtTime\": 1563268933348,\n" +
                "                \"isdel\": false,\n" +
                "                \"startTime\": 0,\n" +
                "                \"endTime\": 0,\n" +
                "                \"updTime\": 1563268933348,\n" +
                "                \"img\": \"\",\n" +
                "                \"orderId\": 10,\n" +
                "                \"name\": \"房车租赁\",\n" +
                "                \"code\": \"RENTRV\",\n" +
                "                \"regulation\": \"房车租赁*1\",\n" +
                "                \"otherRule\": \"{\\\"rule\\\":1‘}\",\n" +
                "                \"integralStatus\": false\n" +
                "            },\n" +
                "            {\n" +
                "                \"id\": 40,\n" +
                "                \"period\": 0,\n" +
                "                \"number\": 1,\n" +
                "                \"point\": 100,\n" +
                "                \"price\": 0,\n" +
                "                \"isContinuity\": false,\n" +
                "                \"finishDay\": 0,\n" +
                "                \"finishPoint\": 0,\n" +
                "                \"status\": true,\n" +
                "                \"crtTime\": 1563268803909,\n" +
                "                \"isdel\": false,\n" +
                "                \"startTime\": 0,\n" +
                "                \"endTime\": 0,\n" +
                "                \"updTime\": 1563268803909,\n" +
                "                \"img\": \"\",\n" +
                "                \"orderId\": 8,\n" +
                "                \"name\": \"参加官方活动\",\n" +
                "                \"code\": \"OFFICIAL\",\n" +
                "                \"regulation\": \"\",\n" +
                "                \"otherRule\": \"\",\n" +
                "                \"integralStatus\": false\n" +
                "            },\n" +
                "            {\n" +
                "                \"id\": 39,\n" +
                "                \"period\": 0,\n" +
                "                \"number\": 100,\n" +
                "                \"point\": 5,\n" +
                "                \"price\": 0,\n" +
                "                \"isContinuity\": false,\n" +
                "                \"finishDay\": 0,\n" +
                "                \"finishPoint\": 0,\n" +
                "                \"status\": true,\n" +
                "                \"crtTime\": 1563268704863,\n" +
                "                \"isdel\": false,\n" +
                "                \"startTime\": 0,\n" +
                "                \"endTime\": 0,\n" +
                "                \"updTime\": 1563269240999,\n" +
                "                \"img\": \"\",\n" +
                "                \"orderId\": 8,\n" +
                "                \"name\": \"评论动态\",\n" +
                "                \"code\": \" COMMENTPUBLISH\",\n" +
                "                \"regulation\": \"\",\n" +
                "                \"otherRule\": \"\",\n" +
                "                \"integralStatus\": false\n" +
                "            },\n" +
                "            {\n" +
                "                \"id\": 38,\n" +
                "                \"period\": 0,\n" +
                "                \"number\": 10,\n" +
                "                \"point\": 50,\n" +
                "                \"price\": 0,\n" +
                "                \"isContinuity\": false,\n" +
                "                \"finishDay\": 0,\n" +
                "                \"finishPoint\": 0,\n" +
                "                \"status\": true,\n" +
                "                \"crtTime\": 1563268680481,\n" +
                "                \"isdel\": false,\n" +
                "                \"startTime\": 0,\n" +
                "                \"endTime\": 0,\n" +
                "                \"updTime\": 1563268680481,\n" +
                "                \"img\": \"\",\n" +
                "                \"orderId\": 7,\n" +
                "                \"name\": \"发表动态\",\n" +
                "                \"code\": \"PUBLISH\",\n" +
                "                \"regulation\": \"\",\n" +
                "                \"otherRule\": \"\",\n" +
                "                \"integralStatus\": false\n" +
                "            },\n" +
                "            {\n" +
                "                \"id\": 37,\n" +
                "                \"period\": 0,\n" +
                "                \"number\": 100,\n" +
                "                \"point\": 0,\n" +
                "                \"price\": 0,\n" +
                "                \"isContinuity\": false,\n" +
                "                \"finishDay\": 0,\n" +
                "                \"finishPoint\": 0,\n" +
                "                \"status\": true,\n" +
                "                \"crtTime\": 1562896686101,\n" +
                "                \"isdel\": false,\n" +
                "                \"startTime\": 0,\n" +
                "                \"endTime\": 0,\n" +
                "                \"updTime\": 1562896686101,\n" +
                "                \"img\": \"https://xxtest.upyuns.com/image/app/2019-07-08/30.png\",\n" +
                "                \"orderId\": 6,\n" +
                "                \"name\": \"购买会员\",\n" +
                "                \"code\": \"BUYMEMBER\",\n" +
                "                \"regulation\": \"会员费*0.5\",\n" +
                "                \"otherRule\": \"{\\\"rule\\\":\\\"1\\\"}\",\n" +
                "                \"btnWord\": \"去看看\",\n" +
                "                \"integralStatus\": false\n" +
                "            },\n" +
                "            {\n" +
                "                \"id\": 36,\n" +
                "                \"period\": 0,\n" +
                "                \"number\": 100,\n" +
                "                \"point\": 10,\n" +
                "                \"price\": 0,\n" +
                "                \"isContinuity\": false,\n" +
                "                \"finishDay\": 0,\n" +
                "                \"finishPoint\": 0,\n" +
                "                \"status\": true,\n" +
                "                \"crtTime\": 1562896278806,\n" +
                "                \"isdel\": false,\n" +
                "                \"startTime\": 0,\n" +
                "                \"endTime\": 0,\n" +
                "                \"updTime\": 1562896278806,\n" +
                "                \"img\": \"https://xxtest.upyuns.com/image/app/2019-07-08/30.png\",\n" +
                "                \"orderId\": 5,\n" +
                "                \"name\": \"图文评论\",\n" +
                "                \"code\": \"COMMENTANDPIC\",\n" +
                "                \"regulation\": \"\",\n" +
                "                \"otherRule\": \"\",\n" +
                "                \"btnWord\": \"去评论\",\n" +
                "                \"integralStatus\": false\n" +
                "            },\n" +
                "            {\n" +
                "                \"id\": 35,\n" +
                "                \"period\": 0,\n" +
                "                \"number\": 100,\n" +
                "                \"point\": 5,\n" +
                "                \"price\": 0,\n" +
                "                \"isContinuity\": false,\n" +
                "                \"finishDay\": 0,\n" +
                "                \"finishPoint\": 0,\n" +
                "                \"status\": true,\n" +
                "                \"crtTime\": 1562896224993,\n" +
                "                \"isdel\": false,\n" +
                "                \"startTime\": 0,\n" +
                "                \"endTime\": 0,\n" +
                "                \"updTime\": 1562896224993,\n" +
                "                \"img\": \"https://xxtest.upyuns.com/image/app/2019-07-08/30.png\",\n" +
                "                \"orderId\": 5,\n" +
                "                \"name\": \"评论\",\n" +
                "                \"code\": \"COMMENT\",\n" +
                "                \"regulation\": \"\",\n" +
                "                \"otherRule\": \"\",\n" +
                "                \"btnWord\": \"去评论\",\n" +
                "                \"integralStatus\": false\n" +
                "            },\n" +
                "            {\n" +
                "                \"id\": 34,\n" +
                "                \"period\": 0,\n" +
                "                \"number\": 1,\n" +
                "                \"point\": 2,\n" +
                "                \"price\": 0,\n" +
                "                \"isContinuity\": true,\n" +
                "                \"finishDay\": 7,\n" +
                "                \"finishPoint\": 7,\n" +
                "                \"status\": true,\n" +
                "                \"crtTime\": 1562895626022,\n" +
                "                \"isdel\": false,\n" +
                "                \"startTime\": 0,\n" +
                "                \"endTime\": 0,\n" +
                "                \"updTime\": 1562895626022,\n" +
                "                \"detail\": \"第一天1分，第二天2分第三天3分........................\",\n" +
                "                \"img\": \"https://xxtest.upyuns.com/image/app/2019-07-08/30.png\",\n" +
                "                \"orderId\": 4,\n" +
                "                \"name\": \"签到\",\n" +
                "                \"code\": \"SIGN\",\n" +
                "                \"regulation\": \"签到每天2分，连续签到7天后每天7分\",\n" +
                "                \"otherRule\": \"\",\n" +
                "                \"btnWord\": \"去签到\",\n" +
                "                \"integralStatus\": false\n" +
                "            },\n" +
                "            {\n" +
                "                \"id\": 33,\n" +
                "                \"period\": 5,\n" +
                "                \"number\": 1,\n" +
                "                \"point\": 5,\n" +
                "                \"price\": 0,\n" +
                "                \"isContinuity\": false,\n" +
                "                \"finishDay\": 0,\n" +
                "                \"finishPoint\": 0,\n" +
                "                \"status\": true,\n" +
                "                \"crtTime\": 1562895500083,\n" +
                "                \"isdel\": false,\n" +
                "                \"startTime\": 0,\n" +
                "                \"endTime\": 0,\n" +
                "                \"updTime\": 1562895500084,\n" +
                "                \"img\": \"https://xxtest.upyuns.com/image/app/2019-07-08/30.png\",\n" +
                "                \"orderId\": 4,\n" +
                "                \"name\": \"绑定微信\",\n" +
                "                \"code\": \"BINDWECHAT\",\n" +
                "                \"regulation\": \"每个账号绑定一次，多次绑定不加积分\",\n" +
                "                \"otherRule\": \"\",\n" +
                "                \"btnWord\": \"去绑定\",\n" +
                "                \"integralStatus\": false\n" +
                "            }\n" +
                "        ]\n" +
                "    },\n" +
                "    \"rel\": true\n" +
                "}";
        PointBean bean = new Gson().fromJson(result, PointBean.class);
        onShowResult(1, bean);
    }

//    /*********
//     * 获取积分列表
//     */
//    private void getPointList() {
//        if (!TextUtils.isEmpty(OkGoUtil.getToken())) {
//            Map<String, Object> header = new HashMap<>();
//            header.put("Authorization", OkGoUtil.getToken());
//            mPresenter.getData(1, ApiConfig.HTTP_URL_POINT_LIST, PointBean.class, header, true);
//        }
//    }


    private void getSignRuler() {
        String ruler = "{\n" +
                "    \"status\": 200,\n" +
                "    \"message\": \"Success\",\n" +
                "    \"data\": {\n" +
                "        \"id\": 34,\n" +
                "        \"period\": 0,\n" +
                "        \"number\": 1,\n" +
                "        \"point\": 2,\n" +
                "        \"price\": 0,\n" +
                "        \"isContinuity\": true,\n" +
                "        \"finishDay\": 7,\n" +
                "        \"finishPoint\": 7,\n" +
                "        \"status\": true,\n" +
                "        \"crtTime\": 1562895626022,\n" +
                "        \"isdel\": false,\n" +
                "        \"startTime\": 0,\n" +
                "        \"endTime\": 0,\n" +
                "        \"updTime\": 1562895626022,\n" +
                "        \"detail\": \"第一天1分，第二天2分第三天3分........................\",\n" +
                "        \"img\": \"https://xxtest.upyuns.com/image/app/2019-07-08/30.png\",\n" +
                "        \"orderId\": 4,\n" +
                "        \"name\": \"签到\",\n" +
                "        \"code\": \"SIGN\",\n" +
                "        \"regulation\": \"签到每天2分，连续签到7天后每天7分\",\n" +
                "        \"otherRule\": \"\",\n" +
                "        \"btnWord\": \"去签到\"\n" +
                "    },\n" +
                "    \"rel\": true\n" +
                "}";

        RulerBean bean = new Gson().fromJson(ruler, RulerBean.class);
        onShowResult(2, bean);
    }

    /*****
     * 获取签到规则
     */
//    private void getSignRuler() {
//        if (!TextUtils.isEmpty(OkGoUtil.getToken())) {
//            Map<String, Object> header = new HashMap<>();
//            header.put("Authorization", OkGoUtil.getToken());
//            mPresenter.getData(2, ApiConfig.HTTP_URL_POINT_LIST, RulerBean.Ruler.class, header, true);
//        }
//    }
    @Override
    public void onShowResult(int requestType, BaseBean result) {

        if (requestType == 1) {

            if (result != null && result instanceof PointBean) {
                PointBean pointBean = (PointBean) result;
                if (pointBean.getData() != null && pointBean.getData().getData() != null) {
                    adapter.setNewData(pointBean.getData().getData());
                }
            }
        }

        if (requestType == 0) {
            if (result != null && result instanceof UserPointInfoBean) {
                UserPointInfoBean pointInfoBean = (UserPointInfoBean) result;
                if (pointInfoBean.getData() != null) {
                    String text = pointInfoBean.getData().getRestPoint() + "\n分";
                    int position = text.indexOf("分");
                    SpannableString sStr = new SpannableString(text);
                    sStr.setSpan(new AbsoluteSizeSpan(DisplayUtil.sp2px(this, 12)), position, position + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    sStr.setSpan(new ForegroundColorSpan(Color.parseColor("#F8F3C9")), position, position + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    tvPoint.setText(sStr);
                }
            }
        }

        if (requestType == 2) {
            if (result != null && result instanceof RulerBean) {
                RulerBean bean = (RulerBean) result;
                if (bean.getData() != null) {
                    mPresenter.showRegulationDialog(PointsActivity.this, bean.getData().getDetail());
                }
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        getUserPointInfo();
        getPointList();
    }
}
