package com.usai.apex.mainframe;


import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.SimpleAdapter;

import com.usai.apex.R;
import com.usai.util.commonUtil;

import org.apache.http.util.EncodingUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 */
public class StaticModelistFragment extends ListFragment {

    String ModeName=null;
    String ModeIcon= null;


//    public class SimpleRoundCornerAdapter extends  SimpleAdapter{
//        public SimpleRoundCornerAdapter(Context context, List<? extends Map<String, ?>> data,
//                                        @LayoutRes int resource, String[] from, @IdRes int[] to) {
//            super(context, data, resource, from, to);
//
//        }
//        @Override
//        public View getView(int position, View convertView, ViewGroup parent) {
//            View v = super.getView(position,convertView,parent);
////            v.setBackground(getResources().getDrawable(R.drawable.list_corner_round_bg));
////            v.setBackgroundColor(Color.BLUE);
//            return v;
//        }
//    }

    SimpleAdapter adapter;




    public StaticModelistFragment() {
        // Required empty public constructor
    }

//
//    @Override
//    public View onCreateView(LayoutInflater inflater, ViewGroup container,
//                             Bundle savedInstanceState) {
//
//
//
//        // Inflate the layout for this fragment
//        return inflater.inflate(R.layout.fragment_static_modelist, container, false);
//
//
////        super.onActivityCreated(savedInstanceState);
//
//
//    }

    protected  void setupAdapter(int rawid)
    {
        JSONObject jsonobj=loadjson(rawid);
        adapter        = new SimpleAdapter(this.getActivity(),
                getData(jsonobj), R.layout.static_modelist_cell, new String[] { "title",
                "img","detail" }, new int[] { R.id.tv_title, R.id.iv_thumb,R.id.tv_detail });

        View headerView = initHeaderView();
        if(headerView!=null) {
//            headerView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//
//                }
//            });
            getListView().addHeaderView(headerView);
        }
        setListAdapter(adapter);
    }

    protected void customize_style()
    {



//        this.getListView().setDivider(new ColorDrawable(Color.RED));
        this.getListView().setDivider(getResources().getDrawable(R.drawable.list_div));
        int px=commonUtil.dp2px(getActivity(), 8.75f);
        this.getListView().setDividerHeight(px);


//        this.getListView().setBackground(getResources().getDrawable(R.drawable.list_corner_round_bg));
        this.getListView().setBackgroundColor(getResources().getColor(R.color.table_bg)  );
//        this.getListView().setSelector(R.drawable.list_corner_round);


    }

    protected View initHeaderView()
    {

        View headerView = LayoutInflater.from(getActivity()).inflate(R.layout.invisable_listheader, null);
        return headerView;

    }
    protected JSONObject loadjson(int rawid)
    {

        String jstring = "";
        try
        {
            InputStream in = getResources().openRawResource(rawid);
            // 获取文件的字节数
            int lenght = in.available();
            // 创建byte数组
            byte[] buffer = new byte[lenght];
            // 将文件中的数据读到byte数组中
            in.read(buffer);
            jstring = EncodingUtils.getString(buffer, "UTF-8");
            JSONObject jsobj = new JSONObject(jstring);
            return jsobj;
//            JSONObject objrecords = jsobj.getJSONObject("records");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;

    }

    protected List<Map<String, Object>> getData(JSONObject jsobj)
    {

        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

        try {
            ModeName=jsobj.getString("title");
            ModeIcon=jsobj.getString("icon");

            JSONArray array = jsobj.getJSONArray("modelist");
             for(int i=0;i<array.length();i++) {

                 JSONObject rowobj = array.getJSONObject(i);

                 Map<String, Object> map = new HashMap<String, Object>();
                 String name=rowobj.getString("name");
                 String info=rowobj.getString("detail");
                 String    icon=rowobj.getString("icon");
                 String    type=rowobj.getString("type");


                 int iconid = commonUtil.iconName2Rid(getContext(),icon);
                 map.put("title", name);
                 map.put("detail", info);
                 map.put("img", iconid);
                 map.put("type", type);
                 list.add(map);


             }


            // jsonobject.put("name", key);
            // jsonobject.put("exp", key+"="+jsonobject.getString("value"));
            // newArray.put(jsonobject);
        } catch (JSONException e) {
            e.printStackTrace();
        }

//        boolean login = false;
//        Bundle b = getArguments();
//
//        if(b!=null)
//        {
//            login = b.getBoolean("Login");
//        }


//        Map<String, Object> map = new HashMap<String, Object>();
//        map.put("title", "Market news");
//        map.put("info", "google 1");
//        map.put("img", R.drawable.rect_market_news);
//        list.add(map);
//
//        map = new HashMap<String, Object>();
//        map.put("title", "Announcements");
//        map.put("info", "google 2");
//        map.put("img", R.drawable.rect_announcements);
//        list.add(map);
//
//        if (login)
//        {
//            map = new HashMap<String, Object>();
//            map.put("title", "Saved Detail/Search");
//            map.put("info", "google 2");
//            map.put("img", R.drawable.rect_search_history);
//            list.add(map);
//
//            map = new HashMap<String, Object>();
//            map.put("title", "Change Password");
//            map.put("img", R.drawable.rect_change_password);
//            list.add(map);
//            map = new HashMap<String, Object>();
//            map.put("title", "Setting");
//            map.put("img", R.drawable.rect_setting);
//            list.add(map);
//        }
//
//        map = new HashMap<String, Object>();
//        map.put("title", "Apex History");
//
//        map.put("img", R.drawable.rect_history);
//        list.add(map);
//
//        map = new HashMap<String, Object>();
//        map.put("title", "About this App");
//        map.put("info", "google 2");
//        map.put("img", R.drawable.rect_about);
//        list.add(map);

        return list;
    }

}
