package org.literacyapp.contentprovider.dao;

import java.util.List;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;
import org.greenrobot.greendao.query.Query;
import org.greenrobot.greendao.query.QueryBuilder;

import java.util.Calendar;
import org.literacyapp.contentprovider.dao.converter.CalendarConverter;
import org.literacyapp.contentprovider.dao.converter.ContentStatusConverter;
import org.literacyapp.contentprovider.dao.converter.LocaleConverter;
import org.literacyapp.contentprovider.dao.converter.SpellingConsistencyConverter;
import org.literacyapp.contentprovider.model.JoinNumbersWithWords;
import org.literacyapp.contentprovider.model.content.multimedia.JoinAudiosWithWords;
import org.literacyapp.contentprovider.model.content.multimedia.JoinImagesWithWords;
import org.literacyapp.contentprovider.model.content.multimedia.JoinVideosWithWords;
import org.literacyapp.model.enums.Locale;
import org.literacyapp.model.enums.content.ContentStatus;
import org.literacyapp.model.enums.content.SpellingConsistency;

import org.literacyapp.contentprovider.model.content.Word;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "WORD".
*/
public class WordDao extends AbstractDao<Word, Long> {

    public static final String TABLENAME = "WORD";

    /**
     * Properties of entity Word.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property Locale = new Property(1, String.class, "locale", false, "LOCALE");
        public final static Property TimeLastUpdate = new Property(2, Long.class, "timeLastUpdate", false, "TIME_LAST_UPDATE");
        public final static Property RevisionNumber = new Property(3, Integer.class, "revisionNumber", false, "REVISION_NUMBER");
        public final static Property ContentStatus = new Property(4, String.class, "contentStatus", false, "CONTENT_STATUS");
        public final static Property Text = new Property(5, String.class, "text", false, "TEXT");
        public final static Property Phonetics = new Property(6, String.class, "phonetics", false, "PHONETICS");
        public final static Property UsageCount = new Property(7, int.class, "usageCount", false, "USAGE_COUNT");
        public final static Property SpellingConsistency = new Property(8, String.class, "spellingConsistency", false, "SPELLING_CONSISTENCY");
    }

    private final LocaleConverter localeConverter = new LocaleConverter();
    private final CalendarConverter timeLastUpdateConverter = new CalendarConverter();
    private final ContentStatusConverter contentStatusConverter = new ContentStatusConverter();
    private final SpellingConsistencyConverter spellingConsistencyConverter = new SpellingConsistencyConverter();
    private Query<Word> audio_WordsQuery;
    private Query<Word> image_WordsQuery;
    private Query<Word> video_WordsQuery;
    private Query<Word> number_WordsQuery;

    public WordDao(DaoConfig config) {
        super(config);
    }
    
    public WordDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"WORD\" (" + //
                "\"_id\" INTEGER PRIMARY KEY ," + // 0: id
                "\"LOCALE\" TEXT NOT NULL ," + // 1: locale
                "\"TIME_LAST_UPDATE\" INTEGER," + // 2: timeLastUpdate
                "\"REVISION_NUMBER\" INTEGER NOT NULL ," + // 3: revisionNumber
                "\"CONTENT_STATUS\" TEXT NOT NULL ," + // 4: contentStatus
                "\"TEXT\" TEXT NOT NULL ," + // 5: text
                "\"PHONETICS\" TEXT NOT NULL ," + // 6: phonetics
                "\"USAGE_COUNT\" INTEGER NOT NULL ," + // 7: usageCount
                "\"SPELLING_CONSISTENCY\" TEXT);"); // 8: spellingConsistency
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"WORD\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, Word entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
        stmt.bindString(2, localeConverter.convertToDatabaseValue(entity.getLocale()));
 
        Calendar timeLastUpdate = entity.getTimeLastUpdate();
        if (timeLastUpdate != null) {
            stmt.bindLong(3, timeLastUpdateConverter.convertToDatabaseValue(timeLastUpdate));
        }
        stmt.bindLong(4, entity.getRevisionNumber());
        stmt.bindString(5, contentStatusConverter.convertToDatabaseValue(entity.getContentStatus()));
        stmt.bindString(6, entity.getText());
        stmt.bindString(7, entity.getPhonetics());
        stmt.bindLong(8, entity.getUsageCount());
 
        SpellingConsistency spellingConsistency = entity.getSpellingConsistency();
        if (spellingConsistency != null) {
            stmt.bindString(9, spellingConsistencyConverter.convertToDatabaseValue(spellingConsistency));
        }
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, Word entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
        stmt.bindString(2, localeConverter.convertToDatabaseValue(entity.getLocale()));
 
        Calendar timeLastUpdate = entity.getTimeLastUpdate();
        if (timeLastUpdate != null) {
            stmt.bindLong(3, timeLastUpdateConverter.convertToDatabaseValue(timeLastUpdate));
        }
        stmt.bindLong(4, entity.getRevisionNumber());
        stmt.bindString(5, contentStatusConverter.convertToDatabaseValue(entity.getContentStatus()));
        stmt.bindString(6, entity.getText());
        stmt.bindString(7, entity.getPhonetics());
        stmt.bindLong(8, entity.getUsageCount());
 
        SpellingConsistency spellingConsistency = entity.getSpellingConsistency();
        if (spellingConsistency != null) {
            stmt.bindString(9, spellingConsistencyConverter.convertToDatabaseValue(spellingConsistency));
        }
    }

    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    @Override
    public Word readEntity(Cursor cursor, int offset) {
        Word entity = new Word( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            localeConverter.convertToEntityProperty(cursor.getString(offset + 1)), // locale
            cursor.isNull(offset + 2) ? null : timeLastUpdateConverter.convertToEntityProperty(cursor.getLong(offset + 2)), // timeLastUpdate
            cursor.getInt(offset + 3), // revisionNumber
            contentStatusConverter.convertToEntityProperty(cursor.getString(offset + 4)), // contentStatus
            cursor.getString(offset + 5), // text
            cursor.getString(offset + 6), // phonetics
            cursor.getInt(offset + 7), // usageCount
            cursor.isNull(offset + 8) ? null : spellingConsistencyConverter.convertToEntityProperty(cursor.getString(offset + 8)) // spellingConsistency
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, Word entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setLocale(localeConverter.convertToEntityProperty(cursor.getString(offset + 1)));
        entity.setTimeLastUpdate(cursor.isNull(offset + 2) ? null : timeLastUpdateConverter.convertToEntityProperty(cursor.getLong(offset + 2)));
        entity.setRevisionNumber(cursor.getInt(offset + 3));
        entity.setContentStatus(contentStatusConverter.convertToEntityProperty(cursor.getString(offset + 4)));
        entity.setText(cursor.getString(offset + 5));
        entity.setPhonetics(cursor.getString(offset + 6));
        entity.setUsageCount(cursor.getInt(offset + 7));
        entity.setSpellingConsistency(cursor.isNull(offset + 8) ? null : spellingConsistencyConverter.convertToEntityProperty(cursor.getString(offset + 8)));
     }
    
    @Override
    protected final Long updateKeyAfterInsert(Word entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    @Override
    public Long getKey(Word entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(Word entity) {
        return entity.getId() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "words" to-many relationship of Audio. */
    public List<Word> _queryAudio_Words(long audioId) {
        synchronized (this) {
            if (audio_WordsQuery == null) {
                QueryBuilder<Word> queryBuilder = queryBuilder();
                queryBuilder.join(JoinAudiosWithWords.class, JoinAudiosWithWordsDao.Properties.WordId)
                    .where(JoinAudiosWithWordsDao.Properties.AudioId.eq(audioId));
                audio_WordsQuery = queryBuilder.build();
            }
        }
        Query<Word> query = audio_WordsQuery.forCurrentThread();
        query.setParameter(0, audioId);
        return query.list();
    }

    /** Internal query to resolve the "words" to-many relationship of Image. */
    public List<Word> _queryImage_Words(long imageId) {
        synchronized (this) {
            if (image_WordsQuery == null) {
                QueryBuilder<Word> queryBuilder = queryBuilder();
                queryBuilder.join(JoinImagesWithWords.class, JoinImagesWithWordsDao.Properties.WordId)
                    .where(JoinImagesWithWordsDao.Properties.ImageId.eq(imageId));
                image_WordsQuery = queryBuilder.build();
            }
        }
        Query<Word> query = image_WordsQuery.forCurrentThread();
        query.setParameter(0, imageId);
        return query.list();
    }

    /** Internal query to resolve the "words" to-many relationship of Video. */
    public List<Word> _queryVideo_Words(long videoId) {
        synchronized (this) {
            if (video_WordsQuery == null) {
                QueryBuilder<Word> queryBuilder = queryBuilder();
                queryBuilder.join(JoinVideosWithWords.class, JoinVideosWithWordsDao.Properties.WordId)
                    .where(JoinVideosWithWordsDao.Properties.VideoId.eq(videoId));
                video_WordsQuery = queryBuilder.build();
            }
        }
        Query<Word> query = video_WordsQuery.forCurrentThread();
        query.setParameter(0, videoId);
        return query.list();
    }

    /** Internal query to resolve the "words" to-many relationship of Number. */
    public List<Word> _queryNumber_Words(long numberId) {
        synchronized (this) {
            if (number_WordsQuery == null) {
                QueryBuilder<Word> queryBuilder = queryBuilder();
                queryBuilder.join(JoinNumbersWithWords.class, JoinNumbersWithWordsDao.Properties.WordId)
                    .where(JoinNumbersWithWordsDao.Properties.NumberId.eq(numberId));
                number_WordsQuery = queryBuilder.build();
            }
        }
        Query<Word> query = number_WordsQuery.forCurrentThread();
        query.setParameter(0, numberId);
        return query.list();
    }

}