package com.usai.redant.Detail;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.usai.redant.CommonEditor.CommonEditorActivity;
import com.usai.redant.redantmobile.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class KVDetailActivity extends AppCompatActivity {

    public static Intent build(Context context,String content_json_str) {

        Intent intent = new Intent(context,KVDetailActivity.class);

        if (content_json_str != null) {
            intent.putExtra("content",content_json_str);
        }

        return intent;
    }

    private JSONObject mContent;
    private Context mContext;
    private KVListAdapter adapter;
    private ExpandableListView listView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_kvdetail);

        mContext = this;

        String content_json_str = getIntent().getStringExtra("content");
        if (content_json_str != null) {
            try {
                mContent = new JSONObject(content_json_str);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        adapter = new KVListAdapter();
        listView = (ExpandableListView) findViewById(R.id.kv_listView);
        listView.setAdapter(adapter);
        // 全部展开
        for (int i = 0; i < adapter.getGroupCount(); i++) {
            listView.expandGroup(i);
        }
    }


    private class KVListAdapter extends BaseExpandableListAdapter {


        int TypeCell = 0;
        int TypeHeader = 1;

        @Override
        public int getGroupCount() {
            if (mContent == null) {
                return 0;
            }
            int count = mContent.optInt("count",0);
            return count;
        }

        @Override
        public int getChildrenCount(int i) {
            JSONObject json = (JSONObject) getGroup(i);
            if (json != null) {
                JSONArray data = json.optJSONArray("data");
                if (data != null) {
                    return data.length();
                }
            }
            return 0;
        }

        @Override
        public Object getGroup(int i) {

            JSONObject section = mContent.optJSONObject("section_" + i);

            return section;
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            JSONObject json = (JSONObject) getGroup(groupPosition);
            if (json != null) {
                JSONArray data = json.optJSONArray("data");
                if (data != null) {
                    return data.opt(childPosition);
                }
            }
            return null;
        }

        @Override
        public int getChildTypeCount() {
            return 2;
        }

        @Override
        public int getChildType(int groupPosition, int childPosition) {
            return TypeCell;
        }

        @Override
        public long getGroupId(int i) {
            return i;
        }

        @Override
        public long getChildId(int i, int i1) {
            return i * 1000 + i1;
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }

        @Override
        public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {

            HeaderHolder holder;

            if (view == null) {

                view = LayoutInflater.from(mContext).inflate(R.layout.kv_list_section_cell,null);
                holder = new HeaderHolder(view);

            } else {
                holder = (HeaderHolder) view.getTag();
            }

            JSONObject json = (JSONObject)getGroup(i);
            if (json != null) {
                holder.titleLabel.setText(json.optString("title"));
            } else {
                holder.titleLabel.setText(null);
            }

            return view;
        }

        @Override
        public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {

            CellHolder holder;
            if (view == null) {

                view = LayoutInflater.from(mContext).inflate(R.layout.kv_list_detail_cell,null);
                holder = new CellHolder(view);

            } else {

                holder = (CellHolder) view.getTag();

            }

            JSONObject item = (JSONObject)getChild(i, i1);
            if (item != null) {
                String key = item.optString("key");
                String val = item.optString("val");
                if (val == null || val.equals("null")) {
                    val = "";
                }

                holder.titleLabel.setText(key);
                holder.valLabel.setText(val);
            } else {
                holder.titleLabel.setText(null);
                holder.valLabel.setText(null);
            }

            return view;
        }

        @Override
        public boolean isChildSelectable(int i, int i1) {
            return false;
        }


        private class HeaderHolder {
            TextView titleLabel;
            HeaderHolder(View view) {
                titleLabel = view.findViewById(R.id.kv_list_section_title_lb);
                view.setTag(this);
            }
        }

        private class CellHolder {
            TextView titleLabel, valLabel;
            CellHolder(View view) {
                titleLabel = view.findViewById(R.id.detail_title_lb);
                valLabel = view.findViewById(R.id.detail_val_lb);
                view.setTag(this);
            }
        }
    }
}
