package com.sheep.gamegroup.view.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kfzs.appstore.utils.adapter.recyclerview.RecyclerViewAdapter;
import com.kfzs.appstore.utils.adapter.recyclerview.ViewHolder;
import com.kfzs.duanduan.utils.NumberFormatUtils;
import com.sheep.gamegroup.di.components.DaggerSignRankingsComponent;
import com.sheep.gamegroup.di.modules.SignRankingsModule;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.PunchEntity;
import com.sheep.gamegroup.presenter.SignRankingsContract;
import com.sheep.gamegroup.presenter.SignRankingsPresenter;
import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.gamegroup.util.ListUtil;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.base.BaseFragment;
import com.sheep.jiuyan.samllsheep.utils.G;

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

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.Unbinder;


/**
 * 本月排行榜
 */
public class FgtSignRankings extends BaseFragment implements SignRankingsContract.View {

    @Inject
    SignRankingsPresenter presenter;
    int type;
    @BindView(R.id.signRankings_RecyclerView)
    RecyclerView signRankingsRecyclerView;
    @BindView(R.id.main_content)
    LinearLayout main_content;

    @BindView(R.id.empty_view)
    View empty_view;
    Unbinder unbinder;
    RecyclerViewAdapter recyclerViewAdapter;

    public static FgtSignRankings getInstance(int type) {
        FgtSignRankings fgtSignRankings = new FgtSignRankings();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        fgtSignRankings.setArguments(bundle);
        return fgtSignRankings;
    }

    private Activity activity;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity = getActivity();
        if (getArguments() != null)
            type = getArguments().getInt("type", 0);
        DaggerSignRankingsComponent.builder()
                .netComponent(SheepApp.getInstance().getNetComponent())
                .signRankingsModule(new SignRankingsModule(this))
                .build()
                .inject(this);

    }

    @Override
    public int getLayoutId() {
        return R.layout.fgt_sign_rankings;
    }

    @Override
    public void onViewCreated() {
        activity = getActivity();
        signRankingsRecyclerView.setLayoutManager(new LinearLayoutManager(activity));
        switch (type) {
            case 0://获取累计打卡
                presenter.getTotalPunchTop();
                break;
            case 1://获取连续打卡
                presenter.getContinuePunchTop();
                break;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        return rootView;
    }

    @Override
    public void succPunch(BaseMessage o) {
        List<PunchEntity> list = o.getDatas(PunchEntity.class);

        if (ListUtil.isEmpty(list)) {
            CommonUtil.getInstance().updateEmptyView(empty_view, true);
            main_content.setVisibility(View.GONE);
        } else {
            empty_view.setVisibility(View.GONE);
            main_content.setVisibility(View.VISIBLE);
            recyclerViewAdapter = new RecyclerViewAdapter<PunchEntity>(SheepApp.getInstance(), R.layout.sign_rankings_item, list) {
                @Override
                public void convert(ViewHolder viewHolder, PunchEntity item, final int position) {
                    ImageView sign_iv_icon = viewHolder.getView(R.id.sign_iv_icon);
                    TextView sign_tv_mc = viewHolder.getView(R.id.sign_tv_mc);
                    TextView sign_tv_name = viewHolder.getView(R.id.sign_tv_name);
                    TextView sign_tv_pm = viewHolder.getView(R.id.sign_tv_pm);
                    TextView sign_tv_bonus = viewHolder.getView(R.id.sign_tv_bonus);
                    switch (position) {
                        case 0:
                            sign_iv_icon.setVisibility(View.VISIBLE);
                            sign_tv_mc.setVisibility(View.GONE);
                            sign_iv_icon.setImageResource(R.mipmap.ask_top_1);
                            break;
                        case 1:
                            sign_iv_icon.setVisibility(View.VISIBLE);
                            sign_tv_mc.setVisibility(View.GONE);
                            sign_iv_icon.setImageResource(R.mipmap.ask_top_2);
                            break;
                        case 2:
                            sign_iv_icon.setVisibility(View.VISIBLE);
                            sign_tv_mc.setVisibility(View.GONE);
                            sign_iv_icon.setImageResource(R.mipmap.ask_top_3);
                            break;
                        default:
                            sign_iv_icon.setVisibility(View.GONE);
                            sign_tv_mc.setVisibility(View.VISIBLE);
                            sign_tv_mc.setText(String.format(Locale.CHINA, "%s", position + 1));
                            break;
                    }
                    sign_tv_bonus.setText(String.format(Locale.CHINA, "%s元", NumberFormatUtils.retainMost2(item.getTotalBonus())));
                    sign_tv_name.setText(item.getNick_name());
                    sign_tv_pm.setText(item.getNum());
                }
            };
            signRankingsRecyclerView.setAdapter(recyclerViewAdapter);
        }

    }

    @Override
    public void failPunch(BaseMessage o) {
        CommonUtil.getInstance().updateEmptyView(empty_view, true);
        main_content.setVisibility(View.GONE);
        G.showToast(o.getMsg());
    }
}
