package com.sharefaith.thesharefaithapp.models;

import com.appideas.base.AiDb;
import com.appideas.base.AiSTr;
import com.sharefaith.thesharefaithapp.base.SFApplication;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by Anthony on 4/28/16.
 * Model for Inbox items
 */
public class SFInboxItemModel
{
    /** Content of Inbox item */
    public String mContent;
    /** type of Inbox item */
    public String mObjectType;
    /** payload of notification */
    public String mPayload;
    /** Date Inbox item was posted */
    public String mPostDate;
    /** Image of Inbox item */
    public String mImage;
    /** External id of Inbox item */
    public String mExternalId;

    /**
     * Constructor
     * @param data
     */
    public SFInboxItemModel( HashMap<String,String> data )
    {
        refreshMembers( data );
    }

    /**
     * Refresh the data
     * @param data
     */
    public void refreshMembers( HashMap<String,String> data )
    {
        this.mContent = "";
        this.mObjectType = "";
        this.mPayload = "";
        this.mPostDate = "";
        this.mImage = "";
        this.mExternalId = "";

        this.mContent = data.get( "content" );
        this.mObjectType = data.get( "objecttype" );
        try
        {
            this.mPayload = URLDecoder.decode( data.get( "payload" ),"UTF-8" );
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        this.mPostDate = data.get( "postdate" );
        this.mImage = data.get( "image" );
        this.mExternalId = data.get( "externalid" );
    }

    /**
     * Get the inbox Items
     * @return
     */
    public static ArrayList<HashMap<String,String>> getAll()
    {
        AiDb db = SFApplication.getInstance().getDb();
        ArrayList<HashMap<String,String>> returnValue = new ArrayList<HashMap<String, String>>();

        String sql =
                "SELECT id, content " +
                        " FROM section_content " +
                        " WHERE ondevice_section_id = " +
                        "   (SELECT id FROM ondevice_sections WHERE sf_tag = 'inbox' ) " +
                        " ORDER BY position_index DESC";
        String[][] results = db.query( sql );
        if( results.length > 0 )
        {
            Integer[] sectionIds = new Integer[results.length];
            String[] sectionContent = new String[results.length];
            for(int i = 0; i < results.length; i++ )
            {
                sectionIds[i] = AiSTr.denullifyInt( results[i][0] );
                try
                {
                    sectionContent[i] = URLDecoder.decode( AiSTr.denullifyString( results[i][1] ), "UTF-8" );
                } catch (Exception e)
                {
                    e.printStackTrace();
                }

            }
            for( int i = 0; i < sectionIds.length; i++ )
            {
                HashMap<String,String> hashMap = new HashMap<String, String>();
                sql =
                "SELECT meta_key, meta_value " +
                        " FROM section_content_meta " +
                        " WHERE section_content_id = " + sectionIds[i];
                results = db.query( sql );
                for( int j = 0; j < results.length; j++)
                {
                    hashMap.put(AiSTr.denullifyString( results[j][0] ),AiSTr.denullifyString( results[j][1] ));
                }
                hashMap.put("content",sectionContent[i]);
                returnValue.add( hashMap );
            }
        }

        return returnValue;
    }
}
