package com.usai.redant.CommonEditor;

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.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;


import com.usai.redant.redantmobile.R;

import org.json.JSONObject;

public class BundleModelActivity extends AppCompatActivity {


    private JSONObject items_json;
    private ListView item_list;
    private Context mCtx = this;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bundle_model);

        setTitle("Bundle");
        item_list = (ListView) findViewById(R.id.bundle_list_view);

        Intent intent = getIntent();
        String items = intent.getStringExtra("bundle_items");
        if (items != null || !items.isEmpty()) {
            try {

                items_json = new JSONObject(items);

                if (item_list != null) {
                    item_list.setAdapter(new Adapter());
                }

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

    }


    public class Adapter extends BaseAdapter {

        /**
         * How many items are in the data set represented by this Adapter.
         *
         * @return Count of items.
         */
        @Override
        public int getCount() {
            if (items_json != null) {
                int count = items_json.optInt("count");
                return count;
            }
            return 0;
        }

        /**
         * Get the data item associated with the specified position in the data set.
         *
         * @param position Position of the item whose data we want within the adapter's
         *                 data set.
         * @return The data at the specified position.
         */
        @Override
        public Object getItem(int position) {
            if (items_json != null) {
                return items_json.optJSONObject("item_" + position);
            }
            return null;
        }

        /**
         * Get the row id associated with the specified position in the list.
         *
         * @param position The position of the item within the adapter's data set whose row id we want.
         * @return The id of the item at the specified position.
         */
        @Override
        public long getItemId(int position) {
            return position;
        }

        /**
         * Get a View that displays the data at the specified position in the data set. You can either
         * create a View manually or inflate it from an XML layout file. When the View is inflated, the
         * parent View (GridView, ListView...) will apply default layout parameters unless you use
         * {@link LayoutInflater#inflate(int, ViewGroup, boolean)}
         * to specify a root view and to prevent attachment to the root.
         *
         * @param position    The position of the item within the adapter's data set of the item whose view
         *                    we want.
         * @param convertView The old view to reuse, if possible. Note: You should check that this view
         *                    is non-null and of an appropriate type before using. If it is not possible to convert
         *                    this view to display the correct data, this method can create a new view.
         *                    Heterogeneous lists can specify their number of view types, so that this View is
         *                    always of the right type (see {@link #getViewTypeCount()} and
         *                    {@link #getItemViewType(int)}).
         * @param parent      The parent that this view will eventually be attached to
         * @return A View corresponding to the data at the specified position.
         */
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            BundleItemHolder holder;

            if (convertView == null) {

                convertView = LayoutInflater.from(mCtx).inflate(R.layout.bundle_item_cell,null);
                holder = new BundleItemHolder(convertView);

            } else {
                holder = (BundleItemHolder) convertView.getTag();
            }

            JSONObject item_json = (JSONObject) getItem(position);
            if (item_json != null) {
                holder.name_tv.setText(item_json.optString("model"));
                if (item_json.optString("unit_price").equals("No Price")) {
                    holder.up_tv.setText("No Price");
                } else {
                    holder.up_tv.setText(String.format("$ %.2f",Float.valueOf(item_json.optString("unit_price"))));
                }
                holder.qty_tv.setText(item_json.optString("modulus"));

                holder.desc_tv.setText(item_json.optString("description"));
            }


            return convertView;
        }

        public class BundleItemHolder {

            public TextView name_tv;
            public TextView desc_tv;
            public TextView qty_tv;
            public TextView up_tv;

            public BundleItemHolder(View cell) {
                name_tv = (TextView) cell.findViewById(R.id.model_name_tv);
                desc_tv = (TextView) cell.findViewById(R.id.model_desc_tv);
                qty_tv = (TextView) cell.findViewById(R.id.model_qty_tv);
                up_tv = (TextView) cell.findViewById(R.id.model_unit_price_tv);
                cell.setTag(this);
            }

        }
    }

}
