/*
 * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.micode.notes.ui;

import android.content.Context;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.appwidget.AppWidgetManager;

import net.micode.notes.R;
import net.micode.notes.data.Notes;
import net.micode.notes.tool.DataUtils;
import net.micode.notes.tool.ResourceParser.NoteItemBgResources;
import net.micode.notes.tool.ResourceParser;
import net.micode.notes.ui.BackgroundQueryHandler;

public class NotesListItem extends LinearLayout {

    private static final String TAG = "NotesListItem";
    private ImageView mAlert;
    private TextView mTitle;
    private TextView mTime;
    private TextView mCallName;
    private NoteItemData mItemData;
    private CheckBox mCheckBox;
    private boolean mIsPrivacyMode;
    private Context mContext;
    private boolean mRootFolder;

    public NotesListItem(Context context) {
        super(context);
        mContext = context;
        inflate(context, R.layout.note_item, this);
        mAlert = (ImageView) findViewById(R.id.iv_alert_icon);
        mTitle = (TextView) findViewById(R.id.tv_title);
        mTime = (TextView) findViewById(R.id.tv_time);
        mCallName = (TextView) findViewById(R.id.tv_name);
        mCheckBox = (CheckBox) findViewById(android.R.id.checkbox);
    }

    public void bind(NoteItemData data, BackgroundQueryHandler handler, boolean choiceMode, boolean isPrivacyMode) {
        mItemData = data;
        if (data.id == Notes.ID_ROOT_FOLDER) {
            mRootFolder = true;
            mTitle.setText(mContext.getString(R.string.menu_move_parent_folder));
            mTitle.setTextAppearance(mContext, R.style.TextAppearancePrimaryItem);
            mAlert.setVisibility(View.GONE);
            mCallName.setVisibility(View.GONE);
            setBackgroundResource(R.drawable.list_selector_background);
        } else if (data.id == Notes.ID_CALL_RECORD_FOLDER) {
            mRootFolder = true;
            mTitle.setText(mContext.getString(R.string.call_record_folder_name));
            mTitle.setTextAppearance(mContext, R.style.TextAppearancePrimaryItem);
            mAlert.setVisibility(View.GONE);
            mCallName.setVisibility(View.GONE);
            setBackgroundResource(R.drawable.list_selector_background);
        } else {
            mRootFolder = false;
            if (data.type == Notes.TYPE_FOLDER) {
                mTitle.setTextAppearance(mContext, R.style.TextAppearancePrimaryItem);
                mAlert.setVisibility(View.GONE);
                mCallName.setVisibility(View.GONE);
                setBackgroundResource(R.drawable.list_selector_background);
            } else {
                mTitle.setTextAppearance(mContext, R.style.TextAppearanceSecondaryItem);
                mAlert.setVisibility(View.VISIBLE);
                if (data.widgetId != AppWidgetManager.INVALID_APPWIDGET_ID
                        && data.widgetType != Notes.TYPE_WIDGET_INVALIDE) {
                    mCallName.setVisibility(View.VISIBLE);
                } else {
                    mCallName.setVisibility(View.GONE);
                }
                setBackgroundResource(R.drawable.list_selector_background);

                if (data.isPrivate) {
                    mTitle.setText("🔒 " + data.snippet);
                } else {
                    mTitle.setText(data.snippet);
                }

                if (data.alertDate > 0) {
                    mTime.setText(DataUtils.formatDateTime(mContext, data.alertDate));
                    mTime.setVisibility(View.VISIBLE);
                } else {
                    mTime.setVisibility(View.GONE);
                }
            }
        }

        if (choiceMode) {
            mCheckBox.setVisibility(View.VISIBLE);
            mCheckBox.setChecked(isChecked());
        } else {
            mCheckBox.setVisibility(View.GONE);
        }

        if (data.bgColorId != ResourceParser.getDefaultBgId(mContext)) {
            setBackgroundResource(ResourceParser.getNoteBgResId(data.bgColorId));
        } else {
            setBackgroundResource(R.drawable.list_selector_background);
        }
    }

    private void setBackground(NoteItemData data) {
        int id = data.getBgColorId();
        if (data.getType() == Notes.TYPE_NOTE) {
            if (data.isSingle() || data.isOneFollowingFolder()) {
                setBackgroundResource(NoteItemBgResources.getNoteBgSingleRes(id));
            } else if (data.isLast()) {
                setBackgroundResource(NoteItemBgResources.getNoteBgLastRes(id));
            } else if (data.isFirst() || data.isMultiFollowingFolder()) {
                setBackgroundResource(NoteItemBgResources.getNoteBgFirstRes(id));
            } else {
                setBackgroundResource(NoteItemBgResources.getNoteBgNormalRes(id));
            }
        } else {
            setBackgroundResource(NoteItemBgResources.getFolderBgRes());
        }
    }

    public NoteItemData getItemData() {
        return mItemData;
    }

    public void setPrivacyMode(boolean isPrivacyMode) {
        mIsPrivacyMode = isPrivacyMode;
        updatePrivacyMode();
    }

    private void updatePrivacyMode() {
        if (mIsPrivacyMode) {
            if (mTitle != null) {
                mTitle.setText("******");
            }
        } else {
            if (mTitle != null && mItemData != null) {
                mTitle.setText(mItemData.getSnippet());
            }
        }
    }

    public boolean isChecked() {
        return mCheckBox != null && mCheckBox.isChecked();
    }

    public NoteItemData getNoteItemData() {
        return mItemData;
    }
}
