package com.bjzc.zjdxj.activity.qfgd;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import com.bjzc.zjdxj.R;
import com.bjzc.zjdxj.activity.qfgd.tree.TreePoint;
import com.bjzc.zjdxj.adapter.DeviceTreeAdapterBase;
import com.bjzc.zjdxj.base.BaseActivity3;
import com.bjzc.zjdxj.bean.DeviceTreeBean;
import com.bjzc.zjdxj.bean.QfsblistRequestBean;
import com.bjzc.zjdxj.contans.Contans;
import com.bjzc.zjdxj.utils.GsonUtils;
import com.bjzc.zjdxj.utils.HttpListener;
import com.bjzc.zjdxj.utils.NohttpUtils;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;
import java.util.ArrayList;
import java.util.List;



/**
 * Created by ruihong on 2018/4/17.
 */

public class DeviceTreeActivity extends BaseActivity3 {

    ListView rlv;
    List<DeviceTreeBean.DataBean> pointList;
    private DeviceTreeAdapterBase deviceTreeAdapter;


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

    @Override
    public void initView() {
        initToolbar("设备列表", "", new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        rlv = findViewById(R.id.rlv);
    }

    @Override
    public void initData() {
        pointList = new ArrayList<>();
        requestDeviceTreeRoot("0");
    }


    private void requestDeviceTreeRoot(String parentId) {

        Log.i("app", Contans.IP + Contans.QFDDVICESUBLISTTREE);
        final Request<String> requset = NoHttp.createStringRequest(Contans.IP + Contans.QFDDVICELIST, RequestMethod.POST);
        requset.setDefineRequestBodyForJson(createJson(parentId));
        NohttpUtils.getInstance().add(this, 0, requset, new HttpListener<String>() {
            @Override
            public void onSucceed(int what, Response<String> response) {
                try {
                    String resutlString = response.get();
                    Log.i("app", resutlString);
                    DeviceTreeBean deviceTreeBean = GsonUtils.GsonToBean(resutlString, DeviceTreeBean.class);
                    Log.i("app", String.valueOf(deviceTreeBean.getData().size()));
                    Log.i("app", String.valueOf(deviceTreeBean.getData().get(0).getISLEAF().equals("1")));
                    if (deviceTreeBean.getState() == 1) {
                        if (deviceTreeBean.getData().size() > 0) {
                            pointList.clear();
                            pointList.addAll(deviceTreeBean.getData());
                            setAdapter();
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailed(int what, Response<String> response) {

            }
        });
    }

    private void requestDeviceTree(TreePoint treePoint) {

        Log.i("app", Contans.IP + Contans.QFDDVICESUBLISTTREE);
        final Request<String> requset = NoHttp.createStringRequest(Contans.IP + Contans.QFDDVICELIST, RequestMethod.POST);
        requset.setDefineRequestBodyForJson(createJson(treePoint.getID()));
        NohttpUtils.getInstance().add(this, 0, requset, new HttpListener<String>() {
            @Override
            public void onSucceed(int what, Response<String> response) {
                try {
                    String resutlString = response.get();
                    Log.i("app", resutlString);
                    DeviceTreeBean deviceTreeBean = GsonUtils.GsonToBean(resutlString, DeviceTreeBean.class);
                    Log.i("app", String.valueOf(deviceTreeBean.getData().size()));
                    Log.i("app", String.valueOf(deviceTreeBean.getData().get(0).getISLEAF().equals("1")));
                    if (deviceTreeBean.getState() == 1) {
                        if (deviceTreeBean.getData().size() > 0) {
                            pointList.addAll(deviceTreeBean.getData());

                            if ("1".equals(treePoint.getISLEAF())) {
                                //点击叶子节点
                                //Toast.makeText(mcontext, getSubmitResult(treePoint), Toast.LENGTH_SHORT).show();
                            } else {
                                //如果点击的是父类

                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailed(int what, Response<String> response) {

            }
        });
    }

    private String createJson(String parentId) {
        QfsblistRequestBean info = new QfsblistRequestBean();
        info.setAction("Q4SBGLCHILD_GET_TREE");
        info.setPARENTID(parentId);
        String json = GsonUtils.GsonString(info);
        Log.i("app", json);
        return json;
    }

    private void setAdapter() {

        deviceTreeAdapter = new DeviceTreeAdapterBase(context, pointList);
        rlv.setAdapter(deviceTreeAdapter);
        rlv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                DeviceTreeBean.DataBean dataBean = pointList.get(position);
                if(dataBean.getISLEAF().equals("1")){
                    Intent resultIntent = new Intent();
                    Bundle bundle = new Bundle();
                    bundle.putParcelable("dataBean", dataBean);
                    resultIntent.putExtras(bundle);
                    setResult(RESULT_OK, resultIntent);
                    finish();
                }else{

                }
            }
        });


//        deviceTreeAdapter.setOnChildItemClickListener(new DeviceTreeAdapter.OnChildItemClickListener() {
//            @Override
//            public void onClick(DeviceTreeBean.DataBean dataBean) {
//
//                if(dataBean.getISLEAF().equals("1")){
//                    Intent resultIntent = new Intent();
//                    Bundle bundle = new Bundle();
//                    bundle.putParcelable("dataBean", dataBean);
//                    resultIntent.putExtras(bundle);
//                    setResult(RESULT_OK, resultIntent);
//                    finish();
//                }else{
//
//                }
//
////                Intent intent = new Intent(ComfirmSblistActivity.this, OtherSblistActivity.class);
////                intent.putExtra("FSBID", dataBean.getSBID());
////                intent.putExtra("SBMC", dataBean.getSBMC());
////                startActivity(intent);
////                finish();
//
//            }
//        });
    }
}