package com.sheep.gamegroup.view.dialog;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Handler;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.OrienteeringDetail;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.gamegroup.util.DataUtil;
import com.sheep.gamegroup.util.Jump2View;
import com.sheep.gamegroup.util.ListUtil;
import com.sheep.gamegroup.util.LogUtil;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.gamegroup.view.activity.ActMyMoney;
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.utils.G;

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

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by realicing on 2018/6/12.
 * realicing@sina.com
 */
public class DialogOrienteeringDetails {
    private Activity activity;
    private int gao;
    public DialogOrienteeringDetails(Activity activity){
        this.activity = activity;
        gao = SheepApp.getInstance().getResources().getDimensionPixelSize(R.dimen.orienteering_item_gao)+1;
        maxShowCount = Math.max(6, G.HEIGHT*3/4/gao);
    }
    private View empty_view;
    private RefreshLayout swipe_container;
    private ListView dialog_list_view;

    private ArrayAdapter<OrienteeringDetail> adapter;
    private List<OrienteeringDetail> list = ListUtil.emptyList();
    public void showOrienteeringDetails() {
        if (activity != null) {
            View dialog_parent = View.inflate(activity, R.layout.dialog_parent, null);
            final AlertDialog dialog = new AlertDialog.Builder(activity, R.style.MyDialogActivityTheme)
                    .setView(dialog_parent)
                    .create();
            TextView dialog_title = dialog_parent.findViewById(R.id.dialog_title);
            final View dialog_close = dialog_parent.findViewById(R.id.dialog_close);
            LinearLayout dialog_center_ll = dialog_parent.findViewById(R.id.dialog_center_ll);
            View view = LayoutInflater.from(activity).inflate(R.layout.dialog_orienteering_details, dialog_center_ll, true);
            dialog_title.setText("定向货币详情");

            empty_view = view.findViewById(R.id.empty_view);
            swipe_container = view.findViewById(R.id.swipe_container);
            dialog_list_view = view.findViewById(R.id.dialog_list_view);
            TextView dialog_btn_left = view.findViewById(R.id.dialog_btn_left);
            TextView dialog_btn_right = view.findViewById(R.id.dialog_btn_right);

            swipe_container.setFooterView(activity, dialog_list_view, R.layout.listview_footer);
            adapter = new ArrayAdapter<OrienteeringDetail>(activity, R.layout.dialog_orienteering_details_item, list){
                @Override
                public boolean convert(int position, View convertView, ViewGroup parent, OrienteeringDetail item) {
                    TextView dialog_label = convertView.findViewById(R.id.dialog_label);
                    TextView dialog_value = convertView.findViewById(R.id.dialog_value);
                    ViewUtil.setText(dialog_label, item.getName());
                    ViewUtil.setText(dialog_value, String.format(Locale.CHINA, "余额%.2f元", item.getBalance()));
                    return true;
                }
            };
            dialog_list_view.setAdapter(adapter);

            swipe_container.setOnRefreshListener(new RefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    refreshData();
                }
            });

            swipe_container.setOnLoadListener(new RefreshLayout.OnLoadListener() {
                @Override
                public void onLoad() {
                    swipe_container.setLoading(false);
                    if(ListUtil.size(list) >= per_page*page){
                        page += 1;
                        initData();
                    }else {

                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                swipe_container.setRefreshing(false);
                            }
                        },1000);
                    }

                }
            });

            dialog_btn_left.setText("确定");
            dialog_btn_left.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            dialog_btn_right.setText("充值");
            dialog_btn_right.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    Jump2View.getInstance().goRechargeAct(activity, null);

                    Jump2View.getInstance()
                            .goGameMakeMoney(activity, 1);
                    dialog.dismiss();
                }
            });
            dialog_close.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });

            dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialogInterface) {
                }
            });
            try {
                dialog.show();
            } catch (Exception e) {
                e.printStackTrace();
            }
            initData();
        }
    }

    private int page = 1;//页数
    private int per_page = DataUtil.PER_PAGE;
    private int maxShowCount;
    private void refreshData() {
        list.clear();
        page = 1;
        initData();
    }
    public void initData() {
        empty_view.setVisibility(View.GONE);
        SheepApp.getInstance().getNetComponent().getApiService().getMyGames(page, per_page)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                            @Override
                            public void onNext(BaseMessage baseMessage) {
                                List<OrienteeringDetail> newList = baseMessage.getDatas(OrienteeringDetail.class);
                                ListUtil.addAll(list, newList);
                                if(ListUtil.isEmpty(list)){
                                    noDatanotify();
                                } else {
                                    empty_view.setVisibility(View.GONE);
                                    dialog_list_view.setVisibility(View.VISIBLE);
                                    int size = Math.min(maxShowCount, ListUtil.size(list));
                                    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, size * gao);
                                    swipe_container.setLayoutParams(layoutParams);
                                    adapter.notifyDataSetChanged();
                                    swipe_container.setRefreshing(false);
                                }
                            }

                            @Override
                            public void onError(BaseMessage baseMessage) {
                                G.showToast(baseMessage);
                                noDatanotify();
                            }
                        });
    }
    private void noDatanotify(){
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 6*gao);
        swipe_container.setLayoutParams(layoutParams);
        CommonUtil.getInstance().updateEmptyView(empty_view, true);
        swipe_container.setRefreshing(false);
        adapter.notifyDataSetChanged();
    }
}
