package com.sheep.gamegroup.view.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sheep.gamegroup.model.entity.AskAward;
import com.sheep.gamegroup.model.entity.AskTop;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.DialogConfig;
import com.sheep.gamegroup.model.entity.UserEntity;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.gamegroup.util.DataUtil;
import com.sheep.gamegroup.util.Jump2View;
import com.sheep.gamegroup.util.ListUtil;
import com.sheep.gamegroup.util.TestUtil;
import com.sheep.gamegroup.util.ViewHolder;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.gamegroup.view.adapter.AdbCommonRecycler;
import com.sheep.gamegroup.view.dialog.DialogNewbieTaskList;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.base.BaseFragment;

import java.util.List;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

import static com.sheep.gamegroup.util.UMConfigUtils.Event.NEWBIE_TASK;

/**
 * Created by ljy on 2018/8/17.
 */

public class FgtAskGetMoneyRank extends BaseFragment {
    @BindView(R.id.ask_top5_title)
    TextView askTop5Title;
    @BindView(R.id.ask_top5_not_you)
    TextView askTop5NotYou;
    @BindView(R.id.ask_top3_tip)
    ImageView askTop3Tip;
    @BindView(R.id.empty_view_img)
    ImageView emptyViewImg;
    @BindView(R.id.empty_view_msg)
    TextView emptyViewMsg;
    @BindView(R.id.empty_view)
    RelativeLayout emptyView;
    @BindView(R.id.ask_top5_list)
    RecyclerView askTop5List;

    private Activity activity;
    private List<AskTop> list = ListUtil.emptyList();
    private AdbCommonRecycler<AskTop> adapter;
    @Override
    public int getLayoutId() {
        return R.layout.fgt_askgetmoney_rank;
    }

    @Override
    public void onViewCreated() {
        ButterKnife.bind(this, mView);
        activity = getActivity();

        initView();
    }

    private void initView() {
        initAskTop5View();
        askTop5NotYou.setVisibility(View.INVISIBLE);
        initData();
    }
    public void initData() {
//        showProgress();

        initAskTop5();
    }

    private void initAskTop5View() {
        list.clear();
        list.add(null);
        askTop5List.setLayoutManager(new LinearLayoutManager(activity));
        //ask_top5_list 解决滑动不流畅问题
        askTop5List.setHasFixedSize(true);
        askTop5List.setNestedScrollingEnabled(false);
        adapter = new AdbCommonRecycler<AskTop>(activity, list) {

            @Override
            public int getItemViewType(int position) {
                return position;
            }

            @Override
            public int getViewIdByType(int type) {
                switch (type) {
                    case 0:
                        return R.layout.x_ask_top5_title;
                    default:
                        return R.layout.x_ask_top5_item;
                }
            }

            @Override
            public void convert(ViewHolder holder, final AskTop item) {
                View itemView = holder.itemView;
                int position = holder.getAdapterPosition();
                if (position == 0) {
                    return;
                }
                TextView x_ask_top5_item_user = itemView.findViewById(R.id.x_ask_top5_item_user);
                TextView x_ask_top5_item_money = itemView.findViewById(R.id.x_ask_top5_item_money);
                TextView x_ask_top5_item_award = itemView.findViewById(R.id.x_ask_top5_item_award);
                ImageView ask_top5_item_iv = itemView.findViewById(R.id.ask_top5_item_iv);

                if (item == null) {//空数据占位置
                    itemView.setVisibility(View.INVISIBLE);
                    return;
                }
                ViewUtil.setText(x_ask_top5_item_user, String.format(Locale.CHINA, "昵称\n%s", item.getNickname()));
                ViewUtil.setText(x_ask_top5_item_money, String.format(Locale.CHINA, "%d", item.getSum_friend()));
                ViewUtil.setText(x_ask_top5_item_award, item.getPrize());
                switch (position) {
                    case 1:
                        ask_top5_item_iv.setImageResource(R.mipmap.ask_top_1);
                        break;
                    case 2:
                        ask_top5_item_iv.setImageResource(R.mipmap.ask_top_2);
                        break;
                    case 3:
                        ask_top5_item_iv.setImageResource(R.mipmap.ask_top_3);
                        break;
                }


            }
        };
        askTop5List.setAdapter(adapter);
    }
    private void initAskTop5() {
        SheepApp.getInstance().getNetComponent().getApiService().getInvitationTop(5)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        List<AskTop> newList = baseMessage.getDataList(AskTop.class);
                        if (ListUtil.isEmpty(newList)) {
                            return;
                        }
                        list.addAll(newList);
                        notifyDataSetChanged();
                        String userId = DataUtil.getInstance().getUserId();
                        boolean hasMe = false;
                        for (AskTop item : newList) {
                            if (TextUtils.equals(userId, item.getUser_id())) {
                                hasMe = true;
                                break;
                            }
                        }
                        askTop5NotYou.setVisibility(hasMe ? View.INVISIBLE : View.VISIBLE);
                    }

                    @Override
                    public void onError(BaseMessage baseMessage) {
                        notifyDataSetChanged();
                        askTop5NotYou.setVisibility(View.VISIBLE);
                    }
                });
    }

    private void notifyDataSetChanged() {
        hideProgress();
        CommonUtil.getInstance().updateEmptyView(emptyView, list.size() == 1, true);
        while (list.size() < 5) {
            list.add(null);
        }
        adapter.notifyDataSetChanged();
    }

    //显示邀请好友排行榜top5的规则的对话框
    public void showAstTop5Tip() {
        ViewUtil.showMsgDialog(activity, new DialogConfig().setTitle("活动说明")
                .setMsg(".每天实时更新排行数据。\n" +
                        ".根据当月已赚赏金（定向货币除外）和邀请人数进行排行\n" +
                        ".每个月前5名可获取排行奖励。\n" +
                        ".每个月月底最后一天24：00可公布结果，获奖的用户需通过“小绵羊领头羊”公众号发送获奖截图证明及收货信息，核对信息真实后，会第一时间发放！\n" +
                        ".本活动最终解释权归成都橙风有量科技有限公司所有！")
                .setMsgGravity(Gravity.START)
                .setTheme(DialogConfig.THEME_PURPLE)
                .setBtnLeftText("我知道了"));
    }


    @OnClick({R.id.ask_top3_tip
    })
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ask_top3_tip://提现
                showAstTop5Tip();
                break;
        }
    }
}
