package com.sharefaith.thesharefaithapp.adapters;

import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.sharefaith.thesharefaithapp.R;
import com.sharefaith.thesharefaithapp.base.SFApplication;
import com.sharefaith.thesharefaithapp.base.SFConfig;
import com.sharefaith.thesharefaithapp.models.SFDayModel;
import com.sharefaith.thesharefaithapp.ui.SFCalendarActivity;

/**
 * Created by sfwebsitedev on 11/4/16.
 * Adapter for Months
 */

public class SFMonthAdapter extends BaseAdapter
{
    private SFApplication mApplication = SFApplication.getInstance();
    private SFConfig mConfig;
    private SFDayModel[] mDayModels;

    /**
     * Constructor
     * @param dayModels
     */
    public SFMonthAdapter( SFDayModel[] dayModels )
    {
        this. mDayModels = dayModels;
        this.mConfig = new SFConfig();
    }

    static class ViewHolder
    {
        TextView mDayOfMonth;
        ImageView mNotification;
        ImageView mCurrentDay;
    }

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

        if( convertView == null )
        {
            LayoutInflater vi = LayoutInflater.from(mApplication);
            convertView = vi.inflate( R.layout.sf_calendar_item, parent, false);
            //Yikes, the cost of inflation
            holder.mDayOfMonth = (TextView) convertView.findViewById(R.id.textView_day_number);
            holder.mNotification = (ImageView) convertView.findViewById( R.id.imageView_calendar_notification );
            holder.mCurrentDay = (ImageView) convertView.findViewById( R.id.imageView_current_day );
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        if( holder.mDayOfMonth != null )
        {
            holder.mDayOfMonth.setText( Integer.toString( this.mDayModels[position].mDayOfMonth ) );
            if( this.mDayModels[position].mIsCurrentMonth)
            {
                String assetColor = mConfig.customFontColorForNavColor();
                if(assetColor.equals( "ffffff" ))
                {
                    assetColor = "808080";
                }
                holder.mDayOfMonth.setTextColor( Color.WHITE );
                if( this.mDayModels[position].mDayOfMonth == mApplication.currentDayIndex &&
                        this.mDayModels[position].mMonth == mApplication.currentMonthIndex &&
                        this.mDayModels[position].mYear == mApplication.currentYearIndex )
                {
                    holder.mCurrentDay.setVisibility( View.VISIBLE );
                    holder.mCurrentDay.setColorFilter( Color.parseColor( "#" + assetColor ) );

                }
                else
                {
                    holder.mCurrentDay.setVisibility( View.GONE );
                }
                if( this.mDayModels[position].mDayOfMonth == mApplication.activeDayIndex &&
                        this.mDayModels[position].mMonth == mApplication.activeMonthIndex &&
                        this.mDayModels[position].mYear == mApplication.activeYearIndex )
                {
                    holder.mDayOfMonth.setBackgroundResource( R.drawable.sf_circle_empty );
                    GradientDrawable drawable = (GradientDrawable) holder.mDayOfMonth.getBackground();
                    drawable.setStroke( 2, Color.parseColor( "#" + assetColor ) );
                    //drawable.setStroke( 2, Color.parseColor( "#" + mConfig.customFontColorForNavColor() ) );
                }
                else
                {
                    holder.mDayOfMonth.setBackgroundResource( R.drawable.sf_empty );
                }
                if( mDayModels[position].mCalendarEvents != null)
                {
                    holder.mNotification.setColorFilter( Color.parseColor( "#" + assetColor ) );
                    holder.mNotification.setVisibility( View.VISIBLE );
                }
                else
                {
                    holder.mNotification.setVisibility( View.GONE );
                }
            }
            else
            {
                holder.mDayOfMonth.setTextColor( Color.GRAY );
                holder.mCurrentDay.setVisibility( View.GONE );
                holder.mDayOfMonth.setBackgroundResource( R.drawable.sf_empty );
                holder.mNotification.setVisibility( View.GONE );
            }
        }

        final ViewHolder finalHolder = holder;
        convertView.setOnClickListener( new View.OnClickListener()
        {
            @Override
            public void onClick( View v )
            {
                if ( mDayModels[position].mIsCurrentMonth && mDayModels[position].mEvents != null )
                {
                    SFCalendarActivity activity = (SFCalendarActivity) mApplication.getCurrentActivity();

                    mApplication.activeDayIndex = mDayModels[position].mDayOfMonth;
                    mApplication.activeMonthIndex = mDayModels[position].mMonth;
                    mApplication.activeYearIndex = mDayModels[position].mYear;
                    //finalHolder.mDayOfMonth.setBackgroundResource( R.drawable.sf_circle_empty );
                    //testing
                    //finalHolder.mCurrentSelectedDay.setColorFilter( Color.RED );
                    activity.mMonthAdapter.notifyDataSetChanged();
                    if( mDayModels[position].mCalendarEvents != null)
                    {
                        activity.populateEventList( mDayModels[position].mCalendarEvents,true );
                    }
                }
            }
        } );
        return convertView;
    }

    /**
     * Set the active Day when chosen
     * @param pos
     * @param offset
     */
    public void setActiveDay(int pos, int offset)
    {
        pos = pos + offset;
        //Log.d( "mtag","hit setActiveDay pos = " + (pos) );
        if ( mDayModels[pos].mIsCurrentMonth && mDayModels[pos].mEvents != null )
        {
            SFCalendarActivity activity = (SFCalendarActivity) mApplication.getCurrentActivity();

            mApplication.activeDayIndex = mDayModels[pos].mDayOfMonth;
            mApplication.activeMonthIndex = mDayModels[pos].mMonth;
            mApplication.activeYearIndex = mDayModels[pos].mYear;
            //finalHolder.mDayOfMonth.setBackgroundResource( R.drawable.sf_circle_empty );
            //testing
            //finalHolder.mCurrentSelectedDay.setColorFilter( Color.RED );
            activity.mMonthAdapter.notifyDataSetChanged();
            if( mDayModels[pos].mCalendarEvents != null)
            {
                activity.populateEventList( mDayModels[pos].mCalendarEvents,false );
            }
        }
    }

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

    @Override
    public int getCount()
    {
        return mDayModels.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;
    }
}
