package com.qhclh.ytzh.index.fumudai;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.qhclh.ytzh.R;
import com.qhclh.ytzh.base.BaseFragment;
import com.qhclh.ytzh.bean.FumudaiBean;
import com.qhclh.ytzh.home.MainActivity;
import com.qhclh.ytzh.home.RpcUrl;
import com.qhclh.ytzh.index.HomepageEvent;
import com.qhclh.ytzh.tasks.ViewOnClickTask;
import com.qhclh.ytzh.utils.RecyclerViewUtil;

import org.forks.jsonrpc.JsonRpcResult;
import org.forks.jsonrpc.RpcFacade;
import org.forks.jsonrpc.RpcObject;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

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

import butterknife.BindView;

/**
 * Created by 青花瓷 on 2017/12/22.
 */

public class FumudaiFragment extends BaseFragment {
    @BindView(R.id.fumudai_banner)
    ImageView fumudai_banner;
    @BindView(R.id.rv_fuwudai)
    RecyclerView rv_fuwudai;

    private List<FumudaiBean> fumudaiBeenList;
    private FumudaiAdapter fumudaiAdapter;
    private GridLayoutManager mLayoutManager;
    @Override
    protected int setLayout() {
        return R.layout.fragment_index_fumudai;
    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
        Glide.with(getActivity())
                .load(R.drawable.fumudaibarimg)
                .error(R.mipmap.ic_launcher_round)
                .placeholder(R.mipmap.ic_launcher_round)
                .into(fumudai_banner);
    }

    @Override
    protected void initData() {
        fumudaiBeenList = new ArrayList<>();
    }

    @Override
    protected void initOper() {
        RecyclerViewUtil util=new RecyclerViewUtil(getActivity(),rv_fuwudai);
        util.setOnItemClickListener(new RecyclerViewUtil.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, long id) {
                Intent intent = new Intent(getActivity(),ThreeTypeActivity.class);
                intent.putExtra("status",fumudaiAdapter.getItemId(position));
                startActivity(intent);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        if (MainActivity.homepageList!=null) {
            if (MainActivity.homepageList.contains("005")) {
                rv_fuwudai.setVisibility(View.VISIBLE);
            } else {
                rv_fuwudai.setVisibility(View.GONE);
            }
            if (MainActivity.homepageList.contains("006")) {
                fumudai_banner.setVisibility(View.VISIBLE);
            } else {
                fumudai_banner.setVisibility(View.GONE);
            }
        }
        HouseTypeCount();
    }

    private void HouseTypeCount() {
        ViewOnClickTask HouseTypeCountTask = new ViewOnClickTask(getActivity(),"加载中...") {
            List<RpcObject> rpcList;
            @Override
            protected void successUI() {
                fumudaiBeenList.clear();
                for (int i=0;i<rpcList.size();i++) {
                    fumudaiBeenList.add(new FumudaiBean(rpcList.get(i).getInt("Status"),  rpcList.get(i).getInt("Number")));
                }

                fumudaiAdapter = new FumudaiAdapter(getActivity(),fumudaiBeenList);
                mLayoutManager=new GridLayoutManager(getActivity(),2,GridLayoutManager.VERTICAL,false);//设置为一个2列的纵向网格布局
                rv_fuwudai.setLayoutManager(mLayoutManager);
                rv_fuwudai.setAdapter(fumudaiAdapter);
            }

            @Override
            public Object call() throws Exception {
                JsonRpcResult result = RpcFacade.rpcCall(RpcUrl.HouseTypeCount);
                rpcList = result.getRpcObjectList(RpcUrl.HouseTypeCount_fanhui);
                return null;
            }
        };
        HouseTypeCountTask.execute();
    }

    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void onEvent(HomepageEvent event){
        if (event.getHomepageList()!=null) {
            if (event.getHomepageList().contains("005")) {
                rv_fuwudai.setVisibility(View.VISIBLE);
            } else {
                rv_fuwudai.setVisibility(View.GONE);
            }
            if (event.getHomepageList().contains("006")) {
                fumudai_banner.setVisibility(View.VISIBLE);
            } else {
                fumudai_banner.setVisibility(View.GONE);
            }
        }

    }

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