package com.sheep.gamegroup.view.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSONArray;
import com.sheep.gamegroup.absBase.AbsObserver;
import com.sheep.gamegroup.event.BigEvent;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.GameAgencyRechargePlatformEntity;
import com.sheep.gamegroup.model.entity.RecyleObj;
import com.sheep.gamegroup.model.entity.RecyleType;
import com.sheep.gamegroup.model.entity.Release_task;
import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.gamegroup.util.ListUtil;
import com.sheep.gamegroup.util.SysAppUtil;
import com.sheep.gamegroup.view.adapter.TryMakeMoneyAdp;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.base.BaseFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

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

import butterknife.BindView;
import rx.functions.Action1;

import static com.sheep.gamegroup.model.entity.TaskEty.TASK_TYPE_GAME_SHEEP;
import static com.sheep.gamegroup.model.entity.TaskEty.TASK_TYPE_GAME_TECENT;
import static com.sheep.gamegroup.model.entity.TaskEty.TASK_TYPE_GAME_XIAO_MI;

/**
 * Created by ljy on 2018/7/16.
 */

public class FgtGameAgencyRecharge extends BaseFragment {
    public static FgtGameAgencyRecharge newInstance(String type) {
        FgtGameAgencyRecharge fgt = new FgtGameAgencyRecharge();
        Bundle bundle = new Bundle();
        bundle.putString("type", type);
        fgt.setArguments(bundle);
        return fgt;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            task_type = bundle.getString("type", "1002" + ",1003" + ",1004");
        }
        EventBus.getDefault().register(this);
    }

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

    private Activity activity;

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

    @Override
    public void onViewCreated() {
        activity = getActivity();
        initView();
        initData();
    }

    @BindView(R.id.title)
    View title;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    @BindView(R.id.empty_view)
    View empty_view;
    @BindView(R.id.check_net_ll)
    View check_net_ll;
    @BindView(R.id.view_list)
    RecyclerView recyclerview;

    private TryMakeMoneyAdp tryMakeMoneyAdp;
    private List<GameAgencyRechargePlatformEntity> platformList = ListUtil.emptyList();//平台充值
    private List<Release_task> releaseEtyLists = new ArrayList<>();//游戏代充

    private String task_type;

    private void initView() {
        title.setVisibility(View.GONE);
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                initData();
                refresh.setRefreshing(false);
            }
        });


        /**
         * recycle view
         */
        recyclerview.setLayoutManager(new LinearLayoutManager(activity));
        tryMakeMoneyAdp = new TryMakeMoneyAdp(activity);

        for (int i = 0; i < 2; i++) {
            tryMakeMoneyAdp.add(RecyleObj.make(RecyleType.NONE, null));
        }
        recyclerview.setAdapter(tryMakeMoneyAdp);
    }

    private void initData() {
        empty_view.setVisibility(View.INVISIBLE);
        SysAppUtil.checkNet(new AbsObserver<Integer>() {
            @Override
            public void onNext(Integer result) {
                if (result != 0) {//无网络
                    if (check_net_ll != null)
                        check_net_ll.setVisibility(View.VISIBLE);
                } else {
                    if (check_net_ll != null)
                        check_net_ll.setVisibility(View.GONE);
                }
            }
        });
        {
            refresh.setRefreshing(true);
            tryMakeMoneyAdp.clear();
            for (int i = 0; i < 2; i++) {
                tryMakeMoneyAdp.add(RecyleObj.make(RecyleType.NONE, null));
            }

            //平台游戏
            platformList.clear();
            if (TextUtils.isEmpty(task_type)) {
                task_type = "1002" + ",1003" + ",1004";
            }
            switch (task_type) {
                case "1002":

                    platformList.add(new GameAgencyRechargePlatformEntity(
                            R.mipmap.game_platform_sheep_icon,
                            "平台游戏充值",
                            "充值有福利",
                            TASK_TYPE_GAME_SHEEP));
                    break;
                case "1003":
                    platformList.add(new GameAgencyRechargePlatformEntity(
                            R.mipmap.game_tencent_icon,
                            "腾讯游戏充值",
                            "充值有福利",
                            TASK_TYPE_GAME_TECENT));

                    break;
                case "1004":

                    platformList.add(new GameAgencyRechargePlatformEntity(
                            R.mipmap.game_xiaomi_icon,
                            "小米游戏充值",
                            "充值有福利",
                            TASK_TYPE_GAME_XIAO_MI));
                    break;
                case "1002" + ",1003" + ",1004":
                default:
                    platformList.add(new GameAgencyRechargePlatformEntity(
                            R.mipmap.game_platform_sheep_icon,
                            "平台游戏充值",
                            "充值有福利",
                            TASK_TYPE_GAME_SHEEP));
                    platformList.add(new GameAgencyRechargePlatformEntity(
                            R.mipmap.game_xiaomi_icon,
                            "小米游戏充值",
                            "充值有福利",
                            TASK_TYPE_GAME_XIAO_MI));
                    platformList.add(new GameAgencyRechargePlatformEntity(
                            R.mipmap.game_tencent_icon,
                            "腾讯游戏充值",
                            "充值有福利",
                            TASK_TYPE_GAME_TECENT));
                    break;

            }

            tryMakeMoneyAdp.add(RecyleObj.make(RecyleType.GAME_AGENCY_PAYMENT_PLATFORM, platformList), 0);
            notifyDataSetChanged();
            if (TextUtils.isEmpty(task_type))
                return;
            CommonUtil.getInstance()
                    .gameTask(activity, task_type, "", new Action1<BaseMessage>() {
                        @Override
                        public void call(BaseMessage baseMessage) {
                            try {
                                List<Release_task> releaseEtyList = JSONArray.parseArray(JSONArray.toJSONString(baseMessage.getData()), Release_task.class);
                                if (releaseEtyList != null) {
                                    releaseEtyLists.clear();
                                    releaseEtyLists.addAll(releaseEtyList);
                                    tryMakeMoneyAdp.add(RecyleObj.make(RecyleType.GAME_AGENCY_PAYMENT, releaseEtyLists), 1);
                                    notifyDataSetChanged();
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
        }
    }

    private void notifyDataSetChanged() {
        if (platformList.isEmpty() && releaseEtyLists.isEmpty())
            empty_view.setVisibility(View.VISIBLE);
        refresh.setRefreshing(false);
        recyclerview.getAdapter().notifyDataSetChanged();
    }

    @Subscribe
    public void onEventMainThread(BigEvent event) {
        switch (event.getEventTypes()) {
            case GENERATION_ACCOUNT_REFRESH:
                initData();
                break;
        }
        CommonUtil.getInstance().setEventResultViewStatus(event, recyclerview);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
