package net.micode.notes.ui;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.AsyncQueryHandler;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import net.micode.notes.R;
import net.micode.notes.data.Notes;
import net.micode.notes.data.Notes.NoteColumns;
import net.micode.notes.tool.DataUtils;

public class RecycleBinActivity extends Activity {
    private static final int FOLDER_NOTE_LIST_QUERY_TOKEN = 0;

    private static final int FOLDER_LIST_QUERY_TOKEN = 1;

    private BackgroundQueryHandler mBackgroundQueryHandler;

    private NotesListAdapter mNotesListAdapter;

    private ListView mNotesListView;

    private long mCurrentFolderId;

    private ContentResolver mContentResolver;

    private static final String TAG = "RecycleActivity";

    private MenuItem menuItem;

    private static final String NORMAL_SELECTION = NoteColumns.PARENT_ID + "=?";

    private static final String ROOT_FOLDER_SELECTION = "(" + NoteColumns.TYPE + "<>"
            + Notes.TYPE_SYSTEM + " AND " + NoteColumns.PARENT_ID + "=?)" + " OR ("
            + NoteColumns.ID + "=" + Notes.ID_CALL_RECORD_FOLDER + " AND "
            + NoteColumns.NOTES_COUNT + ">0)";

    private final static int REQUEST_CODE_OPEN_NODE = 102;
    private final static int REQUEST_CODE_NEW_NODE = 103;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recycle_bin);
        initResources();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK
                && (requestCode == REQUEST_CODE_OPEN_NODE || requestCode == REQUEST_CODE_NEW_NODE)) {
            mNotesListAdapter.changeCursor(null);
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        startAsyncNotesListQuery();
    }

    @SuppressLint("InflateParams")
    private void initResources() {
        mContentResolver = this.getContentResolver();
        mBackgroundQueryHandler = new BackgroundQueryHandler(this.getContentResolver());
        mCurrentFolderId = Notes.ID_TRASH_FOLER;
        mNotesListView = findViewById(R.id.recycle_list);
        mNotesListView.addFooterView(LayoutInflater.from(this).inflate(R.layout.note_list_footer, null),
                null, false);
        mNotesListView.setOnItemClickListener(new OnListItemClickListener());
        mNotesListAdapter = new NotesListAdapter(this);
        mNotesListAdapter.setChoiceMode(true);
        mNotesListView.setAdapter(mNotesListAdapter);
    }

    /**
     * 重置选择器状态
     */
    private void reset_ChoiseMode() {
        mNotesListAdapter.setChoiceMode(false);
        mNotesListAdapter.setChoiceMode(true);
    }


    private void startAsyncNotesListQuery() {
        String selection = (mCurrentFolderId == Notes.ID_ROOT_FOLDER) ? ROOT_FOLDER_SELECTION
                : NORMAL_SELECTION;
        mBackgroundQueryHandler.startQuery(FOLDER_NOTE_LIST_QUERY_TOKEN, null,
                Notes.CONTENT_NOTE_URI, NoteItemData.PROJECTION, selection, new String[]{
                        String.valueOf(mCurrentFolderId)
                }, NoteColumns.TYPE + " DESC," + NoteColumns.MODIFIED_DATE + " DESC");
    }

    @SuppressLint("HandlerLeak")
    private final class BackgroundQueryHandler extends AsyncQueryHandler {
        public BackgroundQueryHandler(ContentResolver contentResolver) {
            super(contentResolver);
        }

        @Override
        protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
            switch (token) {
                case FOLDER_NOTE_LIST_QUERY_TOKEN:
                    mNotesListAdapter.changeCursor(cursor);
                    break;
                case FOLDER_LIST_QUERY_TOKEN:
                    if (cursor != null && cursor.getCount() > 0) {
                        showFolderListMenu(cursor);
                    } else {
                        Log.e(TAG, "Query folder failed");
                    }
                    break;
                default:
            }
        }
    }

    private void showFolderListMenu(Cursor cursor) {
        AlertDialog.Builder builder = new AlertDialog.Builder(RecycleBinActivity.this);
        builder.setTitle(R.string.menu_title_select_folder);
        final FoldersListAdapter adapter = new FoldersListAdapter(this, cursor);
        builder.setAdapter(adapter, (dialog, which) -> {
            DataUtils.batchMoveToFolder(mContentResolver,
                    mNotesListAdapter.getSelectedItemIds(), adapter.getItemId(which));
            Toast.makeText(
                    RecycleBinActivity.this,
                    getString(R.string.format_move_notes_to_folder,
                            mNotesListAdapter.getSelectedCount(),
                            adapter.getFolderName(RecycleBinActivity.this, which)),
                    Toast.LENGTH_SHORT).show();
        });
        builder.show();
    }

    // TODO: 2023/5/26 修改一下删除函数
    private void batchDelete() {
        // delete notes directly
        if (!DataUtils.batchDeleteNotes(mContentResolver, mNotesListAdapter
                .getSelectedItemIds())) {
            Log.e(TAG, "Delete notes error, should not happens");
        }
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent(RecycleBinActivity.this, NotesListActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void onContextMenuClosed(Menu menu) {
        super.onContextMenuClosed(menu);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        return true;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.recyclebin_options, menu);

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {

        menu.clear();
        getMenuInflater().inflate(R.menu.recyclebin_options, menu);
        menuItem = menu.findItem(R.id.select_all);

        return true;
    }

    /**
     * 当顶部菜单项被选择时所执行的函数
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

//      TODO: 2023/5/26  为这两个选项加入弹窗
        if (item.getItemId() == R.id.recover) {
            if (!DataUtils.batchMoveToFolder(mContentResolver,
                    mNotesListAdapter.getSelectedItemIds(), Notes.ID_ROOT_FOLDER))
                Log.e(TAG, "Recover error, should not happens");
        } else if (item.getItemId() == R.id.realdelete) {
            batchDelete();
        } else if (item.getItemId() == R.id.select_all) {
            if (!mNotesListAdapter.isAllSelected()) {
                item.setTitle("deselect_all");
                mNotesListAdapter.selectAll(true);
            } else {
                item.setTitle("selected_all");
                mNotesListAdapter.selectAll(false);
            }
            return true;
        }
        reset_ChoiseMode();
        return true;
    }

    @Override
    public boolean onSearchRequested() {
        startSearch(null, false, null /* appData */, false);
        return true;
    }

    // TODO: 2023/5/26 将title改成一个icon
    private class OnListItemClickListener implements OnItemClickListener {

        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (view instanceof NotesListItem) {
                NoteItemData item = ((NotesListItem) view).getItemData();

                if (item.getType() == Notes.TYPE_NOTE) {
                    position = position - mNotesListView.getHeaderViewsCount();
                    mNotesListAdapter.setCheckedItem(position, !mNotesListAdapter.isSelectedItem(position));

                    if (mNotesListAdapter.isAllSelected())
                        menuItem.setTitle("deselected_all");
                    else if (menuItem.getTitle() == "deselected_all")
                        menuItem.setTitle("selected_all");
                }
            }
        }

    }
}
