package com.usai.redant.raimage.Model;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.usai.redant.raimage.R;

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

public class ManufactureListActivity extends AppCompatActivity {

    private ListView manufacureList;
    private ArrayList<String> dataSource;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manufacture_list);



        ActionBar mActionBar = getSupportActionBar();
        mActionBar.setHomeButtonEnabled(true);
        mActionBar.setDisplayHomeAsUpEnabled(true);
        mActionBar.setTitle("RA Image");

        dataSource=getIntent().getStringArrayListExtra("mlist");

        if(dataSource==null)
            dataSource = new ArrayList<String>();



        /**Data*/

        /***/
        manufacureList = (ListView)findViewById(R.id.manufacture_list);
        ManufactureAdapter adapter = new ManufactureAdapter(this,R.layout.manufacture_cell,dataSource);
        manufacureList.setAdapter(adapter);

        manufacureList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String manufacture = dataSource.get(position);
                Intent intent = new Intent();
                intent.putExtra("manufacture",manufacture);
                setResult(RESULT_OK,intent);
                finish();
            }
        });

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case android.R.id.home: {
                finish();
            }
            break;
        }
        return true;
    }

    class ManufactureAdapter extends ArrayAdapter<String> {
        private int resourceID;
        public ManufactureAdapter(Context ctx,int resourceId,List<String> list) {
            super(ctx,resourceId,list);
            resourceID = resourceId;
        }

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

            String manufacture = dataSource.get(position);

            View cell = null;
            ViewHolder holder = null;
            if (convertView != null) {
                cell = convertView;
                holder = (ViewHolder)cell.getTag();
            } else  {
                cell = LayoutInflater.from(getContext()).inflate(resourceID,null);
                holder = new ViewHolder();
                cell.setTag(holder);
                holder.manufacutre_tv = (TextView) cell.findViewById(R.id.manufacture_cell_tv);
            }

            holder.manufacutre_tv.setText(manufacture);

            return cell;
        }

        class ViewHolder {
            public TextView manufacutre_tv;
        }
    }
}
