package com.sheep.gamegroup.view.fragment;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

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.CommonUtil;
import com.sheep.gamegroup.util.DataUtil;
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.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.base.BaseFragment;

import java.util.List;

import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.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;

    private Activity activity;
    @Override
    public void onViewCreated() {
        activity = getActivity();
        if(activity != null) {
            swipe_container.setFooterView(activity, list_view, R.layout.listview_footer);
            adapter = new ArrayAdapter<ILog>(activity, R.layout.x_my_money_act_layout_item, list){
                @Override
                public boolean convert(int position, View convertView, ViewGroup parent, final 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);
                    final 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);
                    final ImageView my_money_item_recharge_amount_iv = convertView.findViewById(R.id.my_money_item_recharge_amount_iv);
                    final TextView my_money_item_recharge_amount_2 = convertView.findViewById(R.id.my_money_item_recharge_amount_2);
                    my_money_item_recharge_amount.setText(item.getEventText());
                    my_money_item_recharge_amount_2.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);

                    my_money_item_recharge_amount_2.setVisibility(View.GONE);
                    my_money_item_recharge_amount.post(new Runnable() {
                        @Override
                        public void run() {
                            if(!android.text.TextUtils.isEmpty(item.getEventText()) && my_money_item_recharge_amount.getLayout() != null){
                                int ellipis = my_money_item_recharge_amount.getLayout().getEllipsisCount(my_money_item_recharge_amount.getLineCount()-1);
                                if(ellipis > 0){
                                    my_money_item_recharge_amount_iv.setVisibility(View.VISIBLE);
                                }else {
                                    my_money_item_recharge_amount_iv.setVisibility(View.INVISIBLE);
                                }
                            }else {
                                my_money_item_recharge_amount_iv.setVisibility(View.INVISIBLE);
                            }
                        }
                    });

                    my_money_item_recharge_amount_iv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            item.isSelect(!item.getSelect());
                            if(item.getSelect()){
                                my_money_item_recharge_amount_2.setVisibility(View.VISIBLE);
                                my_money_item_recharge_amount_iv.setImageDrawable(activity.getResources().getDrawable(R.mipmap.myprice_off));
                            }else {
                                my_money_item_recharge_amount_2.setVisibility(View.GONE);
                                my_money_item_recharge_amount_iv.setImageDrawable(activity.getResources().getDrawable(R.mipmap.myprice_on));
                            }
                        }
                    });
                    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 * page){
                        page++;
                        initData();
                    }

                }
            });
            initData();
        }
    }

    public int per_page = DataUtil.PER_PAGE;
    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>(SheepApp.getInstance()) {
                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        List<? extends ILog> newList = getDatas(baseMessage);
                        if(newList == null) {
                            baseMessage.setMsg(SheepApp.getInstance().getString(R.string.service_data_error));
                            onError(baseMessage);
                        } else {
                            if(page == 1)
                                list.clear();
                            ListUtil.addAll(list, newList);
                            notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void onError(BaseMessage baseMessage) {
                        notifyDataSetChanged();
                    }
                });
    }

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

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

    private void notifyDataSetChanged() {
        if(empty_view == null || swipe_container == null){
            return;
        }
        CommonUtil.getInstance().updateEmptyView(empty_view, list.isEmpty());
        adapter.notifyDataSetChanged();
        swipe_container.setRefreshing(false);
        swipe_container.setLoading(false);
    }
}
