package epson.print.widgets;

import android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AlphabetIndexer;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import java.util.Vector;

import epson.print.R;

public abstract class AbstractListBuilder extends AbstractBuilder {
    protected BaseAdapter mAdapter;
    protected ContentResolver mContentResolver;
    protected Context mContext;
    protected Cursor mCursor;
    protected Vector<Object> mData;
    private Handler mHandlerObserver = new Handler(new Handler.Callback() {
        public boolean handleMessage(Message message) {
            new BuildData().execute(new Void[0]);
            return false;
        }
    });
    protected FactoryContentObserver mObserver;
    protected String[] mProjection;
    protected String mSelection;
    protected String[] mSelectionArgs;
    protected String mSortOrder;
    protected Uri mUri;

    public abstract void addPrinter(String[] strArr);

    protected abstract void buildData();

    public abstract View buildViewHolder();

    public abstract Vector<View> buildViewHolderContent(View view);

    public abstract void buildViewHolderContentData(int i, Vector<View> vector, Vector<Object> vector2);

    public abstract AlphabetIndexer getIndexer();

    protected abstract void notifyDataChange();

    protected Cursor query() {
        return mContentResolver.query(mUri, mProjection, mSelection, mSelectionArgs, mSortOrder);
    }

    protected void delete() {
        mContentResolver.delete(mUri, mSelection, mSelectionArgs);
    }

    protected class FactoryContentObserver extends ContentObserver {
        Handler mHandler;

        public FactoryContentObserver(Handler handler) {
            super(handler);
            mHandler = handler;
        }

        public void onChange(boolean z) {
            super.onChange(z);
            mHandler.sendEmptyMessage(0);
        }
    }

    protected void registerObserver() {
        mContext.getContentResolver().registerContentObserver(mUri, true, mObserver);
    }

    protected void triggerEmptyView() {
        TextView textView = (TextView) mLayout.findViewById(16908292);
        if (mData.size() > 0) {
            textView.setVisibility(8);
            return;
        }
        textView.setVisibility(0);
        mLayout.findViewById(16908298).setVisibility(8);
    }

    public AbstractListBuilder(Context context, ViewGroup viewGroup) {
        super(context, viewGroup);
        mContext = context;
        mData = new Vector<>();
        mContentResolver = mContext.getContentResolver();
        mObserver = new FactoryContentObserver(mHandlerObserver);
    }

    public BaseAdapter getAdapter() {
        return mAdapter;
    }

    private class BuildData extends AsyncTask<Void, Void, Void> {
        private BuildData() {
        }

        protected void onPreExecute() {
            super.onPreExecute();
            mLayout.findViewById(16908298).setVisibility(8);
        }

        protected void onPostExecute(Void voidR) {
            super.onPostExecute(voidR);
            mLayout.findViewById(16908298).setVisibility(0);
            mLayout.postInvalidate();
            mAdapter.notifyDataSetChanged();
            notifyDataChange();
        }

        protected Void doInBackground(Void... voidArr) {
            synchronized  (mData) {
                buildData();
            }
            return null;
        }
    }

    public void build() {
        buildLayout(mParentView);
    }

    public void refresh() {
        buildData();
        mAdapter.notifyDataSetChanged();
    }

    public Vector<Object> getData() {
        return mData;
    }

    protected void buildLayout(ViewGroup viewGroup) {
        mLayout = (ViewGroup) mLayoutInflater.inflate(R.layout.mylistview, viewGroup);
        ListView listView = (ListView) mLayout.findViewById(16908298);
        mAdapter = new MyAdapter(this);
        listView.setAdapter(mAdapter);
        LinearLayout linearLayout = new LinearLayout(mLayout.getContext());
        linearLayout.setVisibility(8);
        listView.setEmptyView(linearLayout);
    }

    public void destructor() {
        mData.clear();
    }

    public ListView getListView() {
        return (ListView) mLayout.findViewById(16908298);
    }
}
