package com.sheep.gamegroup.view.fragment;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
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.LinearLayout;
import android.widget.TextView;

import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.Welfare;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.gamegroup.util.Jump2View;
import com.sheep.gamegroup.util.ListUtil;
import com.sheep.gamegroup.util.ViewHolder;
import com.sheep.gamegroup.view.adapter.AdbCommonRecycler;
import com.sheep.gamegroup.view.adapter.DownloadWelfareAdapter;
import com.sheep.gamegroup.view.adapter.GiftCenterAdapter;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.base.BaseFragment;

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

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

/**
 * Created by realicing on 2018/8/28.
 * realicing@sina.com
 */
public class FgtWelfareCenter extends BaseFragment {
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    @BindView(R.id.wc_hot_welfare_list)
    RecyclerView wcHotWelfareList;
    @BindView(R.id.wc_all_welfare)
    TextView wcAllWelfare;
    @BindView(R.id.wc_download_welfare_list)
    RecyclerView wcDownloadWelfareList;
    @BindView(R.id.wc_all_gift)
    TextView wcAllGift;
    @BindView(R.id.wc_gift_center_list)
    RecyclerView wcGiftCenterList;
    @BindView(R.id.bottom_line_text)
    TextView bottomLineText;
    @BindView(R.id.bottom_line)
    LinearLayout bottomLine;
    Unbinder unbinder;

    @Override
    public int getLayoutId() {
        return R.layout.fgt_welfare_center;
    }
    private List<Welfare> hotWelfareList = ListUtil.emptyList();
    private List<Welfare> downloadWelfareList = ListUtil.emptyList();
    private List<Welfare> giftCenterList = ListUtil.emptyList();
    private Activity activity;
    @Override
    public void onViewCreated() {
        activity = getActivity();
        Context context = SheepApp.getInstance();
        //热门福利
        LinearLayoutManager hotLayoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
        wcHotWelfareList.setHasFixedSize(true);
        wcHotWelfareList.setNestedScrollingEnabled(false);
        wcHotWelfareList.setLayoutManager(hotLayoutManager);
        wcHotWelfareList.setAdapter(new AdbCommonRecycler<Welfare>(context, hotWelfareList) {
            @Override
            public int getViewIdByType(int type) {
                return R.layout.item_hot_welfare;
            }

            @Override
            public void convert(ViewHolder holder, Welfare welfareCenter) {
//                ImageView item_hot_welfare_iv = holder.getView(R.id.item_hot_welfare_iv);
//                TextView item_hot_welfare_tv = holder.getView(R.id.item_hot_welfare_tv);
//                if(welfareCenter == null){
//                    ViewUtil.setDefaultText(item_hot_welfare_tv);
//                } else {
//                    //TODO 加载数据
//                    ViewUtil.setText(item_hot_welfare_tv);
//                }
            }
        });
        //下载福利
        LinearLayoutManager downloadLayoutManager = new LinearLayoutManager(context);
        wcDownloadWelfareList.setHasFixedSize(true);
        wcDownloadWelfareList.setNestedScrollingEnabled(false);
        wcDownloadWelfareList.setLayoutManager(downloadLayoutManager);
        wcDownloadWelfareList.setAdapter(new DownloadWelfareAdapter(activity, downloadWelfareList));
        //礼包中心
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        wcGiftCenterList.setHasFixedSize(true);
        wcGiftCenterList.setNestedScrollingEnabled(false);
        wcGiftCenterList.setLayoutManager(layoutManager);
        wcGiftCenterList.setAdapter(new GiftCenterAdapter(activity, giftCenterList));
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshHotWelfare = true;
                refreshDownloadWelfare = true;
                refreshGiftCenter = true;
                refreshData();
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        refreshData();
    }
    private boolean refreshHotWelfare = true;
    private boolean refreshDownloadWelfare = true;
    private boolean refreshGiftCenter = true;
    //刷新数据
    private void refreshData() {
        if(refreshHotWelfare || refreshDownloadWelfare || refreshGiftCenter) {
            refresh.setRefreshing(true);
        }
        //热门福利
        if(refreshHotWelfare) {
            SheepApp.getInstance().getNetComponent().getApiService().getFindTagList()
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                        @Override
                        public void onNext(BaseMessage baseMessage) {
                            refreshHotWelfare = false;
                            ArrayList<Welfare> newList = baseMessage.getDataList(Welfare.class);
                            hotWelfareList.clear();
                            ListUtil.addAll(hotWelfareList, newList);
                            wcHotWelfareList.getAdapter().notifyDataSetChanged();
                            notifyDataSetChanged();
                        }

                        @Override
                        public void onError(BaseMessage baseMessage) {
                            refreshHotWelfare = false;
                            hotWelfareList.clear();
                            wcHotWelfareList.getAdapter().notifyDataSetChanged();
                            notifyDataSetChanged();
                        }
                    });
        }
        //下载福利
        if(refreshDownloadWelfare) {
            SheepApp.getInstance().getNetComponent().getApiService().getFindTagList()
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                        @Override
                        public void onNext(BaseMessage baseMessage) {
                            refreshDownloadWelfare = false;
                            ArrayList<Welfare> newList = baseMessage.getDataList(Welfare.class);
                            downloadWelfareList.clear();
                            ListUtil.addAll(downloadWelfareList, newList);
                            wcDownloadWelfareList.getAdapter().notifyDataSetChanged();
                            notifyDataSetChanged();
                        }

                        @Override
                        public void onError(BaseMessage baseMessage) {
                            refreshDownloadWelfare = false;
                            downloadWelfareList.clear();
                            wcDownloadWelfareList.getAdapter().notifyDataSetChanged();
                            notifyDataSetChanged();
                        }
                    });
        }
        //礼包中心
        if(refreshGiftCenter) {
            SheepApp.getInstance().getNetComponent().getApiService().getFindTagList()
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                        @Override
                        public void onNext(BaseMessage baseMessage) {
                            refreshGiftCenter = false;
                            ArrayList<Welfare> newList = baseMessage.getDataList(Welfare.class);
                            giftCenterList.clear();
                            ListUtil.addAll(giftCenterList, newList);
                            wcGiftCenterList.getAdapter().notifyDataSetChanged();
                            notifyDataSetChanged();
                        }

                        @Override
                        public void onError(BaseMessage baseMessage) {
                            refreshGiftCenter = false;
                            giftCenterList.clear();
                            wcGiftCenterList.getAdapter().notifyDataSetChanged();
                            notifyDataSetChanged();
                        }
                    });
        }
    }

    private void notifyDataSetChanged() {
        if(!refreshHotWelfare && !refreshDownloadWelfare && !refreshGiftCenter)
            refresh.setRefreshing(false);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.wc_all_welfare, R.id.wc_all_gift})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.wc_all_welfare://全部福利
                Jump2View.getInstance().goWelfareList(activity);
                break;
            case R.id.wc_all_gift://全部礼包
                Jump2View.getInstance().goGiftCenter(activity);
                break;
        }
    }
}
