package com.kfzs.duanduan.fragment;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.ILog;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.gamegroup.util.ListUtil;
import com.sheep.gamegroup.util.TimeUtil;
import com.sheep.gamegroup.view.adapter.ArrayAdapter;
import com.sheep.gamegroup.view.customview.RefreshLayout;
import com.sheep.jiuyan.samllsheep.BuildConfig;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.base.BaseFragment;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 我的资产 --> 充值记录
 * Created by realicing on 2018/5/8.
 * realicing@sina.com
 */
public abstract class AbsFgtMyMoney extends BaseFragment {
    @BindView(R.id.list_view)
    ListView list_view;
    @BindView(R.id.swipe_container)
    RefreshLayout swipe_container;
    @BindView(R.id.empty_view)
    View empty_view;
    @Override
    public int getLayoutId() {
        return R.layout.list_has_empty;
    }

    private List<ILog> list = ListUtil.emptyList();
    private ArrayAdapter<ILog> adapter;
    @Override
    public void onViewCreated() {
        ButterKnife.bind(this, mView);
        if(getActivity() != null) {
            swipe_container.setFooterView(getActivity(), list_view, R.layout.listview_footer);
            adapter = new ArrayAdapter<ILog>(getActivity(), R.layout.x_my_money_act_layout_item, list){
                @Override
                public boolean convert(int position, View convertView, ViewGroup parent, ILog item) {
                    View my_money_item_top = convertView.findViewById(R.id.my_money_item_top);
                    View my_money_item_center = convertView.findViewById(R.id.my_money_item_center);
                    View my_money_item_bottom = convertView.findViewById(R.id.my_money_item_bottom);
                    TextView my_money_item_recharge_amount = convertView.findViewById(R.id.my_money_item_recharge_amount);
                    TextView my_money_item_recharge_status = convertView.findViewById(R.id.my_money_item_recharge_status);
                    TextView my_money_item_recharge_time = convertView.findViewById(R.id.my_money_item_recharge_time);
                    my_money_item_recharge_amount.setText(item.getEventText());
                    my_money_item_recharge_status.setText(item.getStateText());
                    my_money_item_recharge_status.setTextColor(item.isEventSuccess() ? 0xff282828 : 0xffff2e4b);
                    my_money_item_recharge_time.setText(TimeUtil.TimeStamp2Date(item.getEventTime(), "yyyy/MM/dd HH:mm"));
                    boolean isFirst = position == 0;
                    boolean isLast = ListUtil.isLastPosition(list, position);
                    my_money_item_top.setVisibility(isFirst ? View.VISIBLE : View.GONE);
                    my_money_item_center.setBackgroundColor(position % 2 == 1 ? 0xffF8F8F8 : 0xffffffff);
                    my_money_item_bottom.setVisibility(isLast ? View.VISIBLE : View.GONE);
                    return true;
                }
            };
            list_view.setAdapter(adapter);
            swipe_container.setOnRefreshListener(new RefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    list.clear();
                    adapter.notifyDataSetChanged();
                    page = 1;
                    initData();
                }
            });

            swipe_container.setOnLoadListener(new RefreshLayout.OnLoadListener() {
                @Override
                public void onLoad() {
                    swipe_container.setLoading(false);
                    if(list.size() % per_page == 0)
                        page++;
                    initData();
                }
            });
            initData();
        }
    }

    public static int per_page = 10;
    private int page = 1;
    private void initData() {
        empty_view.setVisibility(View.INVISIBLE);
        initObservable(page, per_page).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(getContext()) {
                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        List<? extends ILog> newList = getDatas(baseMessage);
                        if(newList == null) {
                            baseMessage.setMsg("服务器数据错误");
                            onError(baseMessage);
                        } else {
                            ListUtil.addAll(list, newList);
                            notifyDataSetChanged();
                            if (BuildConfig.DEBUG)
                                System.out.println("baseMessage onNext " + JSON.toJSONString(baseMessage));
                        }
                        if(BuildConfig.DEBUG)
                            System.out.println("baseMessage onNext "+ JSON.toJSONString(baseMessage));
                    }

                    @Override
                    public void onError(BaseMessage baseMessage) {
                        if(BuildConfig.DEBUG)
                            System.out.println("baseMessage onError "+JSON.toJSONString(baseMessage));
                        notifyDataSetChanged();
                    }
                });
    }

    protected abstract List<? extends ILog> getDatas(BaseMessage baseMessage);

    protected abstract Observable<BaseMessage> initObservable(int page, int per_page);

    private void notifyDataSetChanged() {
        empty_view.setVisibility(list.isEmpty() ? View.VISIBLE : View.INVISIBLE);
        adapter.notifyDataSetChanged();
        swipe_container.setRefreshing(false);
        swipe_container.setLoading(false);
    }
}
