package com.sharefaith.thesharefaithapp.adapters;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.sharefaith.thesharefaithapp.R;
import com.sharefaith.thesharefaithapp.base.SFAppData;
import com.sharefaith.thesharefaithapp.base.SFApplication;
import com.sharefaith.thesharefaithapp.base.SFBibleDownloader;
import com.sharefaith.thesharefaithapp.base.SFConfig;
import com.sharefaith.thesharefaithapp.base.SFConstants;
import com.sharefaith.thesharefaithapp.models.SFBibleModel;
import com.sharefaith.thesharefaithapp.ui.SFBibleActivity;

import java.io.File;

/**
 * Created by sfwebsitedev on 3/18/16.
 */
public class SFBibleVersionAdapter extends BaseAdapter
{
    private SFApplication mApplication = SFApplication.getInstance();
    private SFBibleModel[] mItems;
    private SFConfig mConfig;
    private SFAppData appData;

    /**
     * constructor
     * @param items
     */
    public SFBibleVersionAdapter( SFBibleModel[] items )
    {
        this.mItems = items;
        this.mConfig = new SFConfig();
        this.appData = new SFAppData();
    }

    static class ViewHolder
    {
        TextView versionAbreviation;
        TextView versionLong;
        ImageView downloadImage;
    }

    /**
     *
     * @param position
     * @param convertView
     * @param parent
     * @return
     */
    @Override
    public View getView( final int position, View convertView, ViewGroup parent )
    {
        //Holds view info:
        ViewHolder holder = new ViewHolder();

        if( convertView == null )
        {
            LayoutInflater vi = LayoutInflater.from(mApplication);
            convertView = vi.inflate( R.layout.sf_bible_version_frame, null);
            //Yikes, the cost of inflation
            holder.versionAbreviation = (TextView) convertView.findViewById(R.id.version_abreviation_textView);
            holder.versionLong = (TextView) convertView.findViewById( R.id.version_textView );
            holder.downloadImage = (ImageView) convertView.findViewById( R.id.download_imageView );
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        final SFBibleModel currentObject = this.mItems[position];

        if (currentObject != null) //Fill in variable info, constructing view or not:
        {
            if( holder.versionAbreviation != null )
            {
                holder.versionAbreviation.setText( currentObject.mAbreviation.toUpperCase() );
            }

            if( holder.versionLong != null )
            {
                holder.versionLong.setText( currentObject.mFullName );
            }

            if( currentObject.mIsDownloaded)
            {
               holder.downloadImage.setImageResource( R.drawable.sf_circle_checkmark );
            }
            else
            {
                holder.downloadImage.setImageResource( R.drawable.sf_circle_download );
            }
            final ViewHolder finalHolder = holder;
            holder.downloadImage.setOnClickListener( new View.OnClickListener()
            {
                @Override
                public void onClick( View v )
                {
                    if(!currentObject.mIsDownloaded )
                    {
                        if(mApplication.isDownloadingBible)
                        {
                            mApplication.makeToast( mApplication.getString( R.string.alert_wait_for_download ) );
                        }
                        else
                        {
                            mItems[position].mIsDownloaded = true;
                            appData.setDataForVersionDownload(mItems[position].mAbreviation);
                            //Log.d("mTag","downloaded = " + currentObject.mIsDownloaded);
                            mApplication.isDownloadingBible = true;
                            mApplication.bibleIndex = position;
                            SFBibleDownloader downloader = new SFBibleDownloader();
                            downloader.downloadBible( mApplication.getString( R.string.sf_appbible_url )
                                    + SFConstants.UAID_KEY + "/download/" + mItems[position].mAbreviation );
                            //log Analytics
                            mApplication.logEvent( "bible_download","download", mItems[position].mAbreviation );
                            SFBibleActivity activity = (SFBibleActivity) mApplication.getCurrentActivity();
                            activity.toggleBibleVersion();
                            activity.setVersionText();
                            activity.refreshView();
                            notifyDataSetChanged();
                        }
                    }
                }
            } );

        }
        return convertView;
    }

    /**
     *  Actions to get number of items in arraylist
     *
     * @since		20120106
     */

    @Override
    public int getCount()
    {
        return this.mItems.length;
    }

    /**
     *  Actions to get position of item
     *
     * @since		20120106
     */

    @Override
    public Object getItem( int position )
    {
        return null;
    }

    /**
     *  Actions to get item ID
     *
     * @since		20120106
     */

    @Override
    public long getItemId( int id )
    {
        return 0;
    }

    /**
     * Defines the onClick handler for a listview
     *
     * @since		20120106
     */
    public AdapterView.OnItemClickListener handleTap( final SFBibleActivity context )
    {
        final SFApplication application = SFApplication.getInstance();

        return new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick( AdapterView<?> parent, View view, int position, long id )
            {
                application.bibleIndex = position;
                context.toggleBibleVersion();
                context.setVersionText();
                context.refreshView();
            }
        };
    }

    /**
     * Listener to handle long clicks of an item. Allows deletion of local Bible version files.
     * @param context
     * @return
     */
    public AdapterView.OnItemLongClickListener handleLongPress( final SFBibleActivity context )
    {
        return new AdapterView.OnItemLongClickListener()
        {
            @Override
            public boolean onItemLongClick( AdapterView<?> parent, View view, int position, long id )
            {
                if( mApplication.isDownloadingBible )
                {
                    mApplication.makeToast( "Please wait for download to finish" );
                }
                else if( mItems[position].mIsDownloaded )
                {
                    File file = new File( mApplication.getFilesDir() + "/" + mApplication.getString( R.string.sf_bibles_path ) + "/" +
                            mItems[position].mAbreviation );
                    context.deleteBibleOverlay( file, position );
                }
                return true;
            }
        };
    }
}
