/*
 * 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.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TextView;
import net.micode.notes.R;

/**
 * 布局设置对话框
 * <p>
 * 提供布局类型选择、网格列数和项目间距的配置界面。
 * 支持实时预览布局效果。
 * </p>
 */
public class LayoutSettingsDialog {
    private Context mContext;
    private LayoutManagerController mLayoutManagerController;
    private AlertDialog mDialog;
    private Spinner mLayoutTypeSpinner;
    private SeekBar mGridColumnsSeekBar;
    private SeekBar mItemSpacingSeekBar;
    private TextView mGridColumnsValue;
    private TextView mItemSpacingValue;

    /**
     * 构造函数
     * @param context 上下文
     * @param layoutManagerController 布局管理器
     */
    public LayoutSettingsDialog(Context context, LayoutManagerController layoutManagerController) {
        mContext = context;
        mLayoutManagerController = layoutManagerController;
    }

    /**
     * 显示布局设置对话框
     */
    public void show() {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setTitle(R.string.layout_settings_title);

        View dialogView = LayoutInflater.from(mContext).inflate(R.layout.layout_settings_dialog, null);
        builder.setView(dialogView);

        initViews(dialogView);
        setupListeners();

        builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                saveSettings();
            }
        });

        builder.setNegativeButton(android.R.string.cancel, null);

        mDialog = builder.create();
        mDialog.show();
    }

    /**
     * 初始化视图
     * @param dialogView 对话框视图
     */
    private void initViews(View dialogView) {
        mLayoutTypeSpinner = (Spinner) dialogView.findViewById(R.id.layout_type_spinner);
        mGridColumnsSeekBar = (SeekBar) dialogView.findViewById(R.id.grid_columns_seekbar);
        mItemSpacingSeekBar = (SeekBar) dialogView.findViewById(R.id.item_spacing_seekbar);
        mGridColumnsValue = (TextView) dialogView.findViewById(R.id.grid_columns_value);
        mItemSpacingValue = (TextView) dialogView.findViewById(R.id.item_spacing_value);

        // 设置布局类型选项
        ArrayAdapter<LayoutType> layoutAdapter = new ArrayAdapter<>(
                mContext, android.R.layout.simple_spinner_item, LayoutType.values());
        mLayoutTypeSpinner.setAdapter(layoutAdapter);

        // 设置当前值
        LayoutType currentLayout = mLayoutManagerController.getCurrentLayoutType();
        mLayoutTypeSpinner.setSelection(currentLayout.ordinal());

        int gridColumns = mLayoutManagerController.getGridSpanCount();
        mGridColumnsSeekBar.setProgress(gridColumns - 1);
        mGridColumnsValue.setText(String.valueOf(gridColumns));

        int itemSpacing = mLayoutManagerController.getItemSpacing();
        mItemSpacingSeekBar.setProgress(itemSpacing / 2);
        mItemSpacingValue.setText(String.valueOf(itemSpacing));

        // 根据布局类型启用/禁用控件
        updateControlStates(currentLayout);
    }

    /**
     * 设置监听器
     */
    private void setupListeners() {
        mLayoutTypeSpinner.setOnItemSelectedListener(new android.widget.AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(android.widget.AdapterView<?> parent, View view, int position, long id) {
                LayoutType selectedLayout = (LayoutType) parent.getItemAtPosition(position);
                updateControlStates(selectedLayout);
            }

            @Override
            public void onNothingSelected(android.widget.AdapterView<?> parent) {
            }
        });

        mGridColumnsSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                int columns = progress + 1;
                mGridColumnsValue.setText(String.valueOf(columns));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });

        mItemSpacingSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                int spacing = progress * 2;
                mItemSpacingValue.setText(String.valueOf(spacing));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });
    }

    /**
     * 更新控件状态
     * @param layoutType 布局类型
     */
    private void updateControlStates(LayoutType layoutType) {
        switch (layoutType) {
            case LINEAR:
                mGridColumnsSeekBar.setEnabled(false);
                mItemSpacingSeekBar.setEnabled(false);
                break;
            case GRID:
            case STAGGERED:
                mGridColumnsSeekBar.setEnabled(true);
                mItemSpacingSeekBar.setEnabled(true);
                break;
        }
    }

    /**
     * 保存设置
     */
    private void saveSettings() {
        LayoutType selectedLayout = (LayoutType) mLayoutTypeSpinner.getSelectedItem();
        int gridColumns = mGridColumnsSeekBar.getProgress() + 1;
        int itemSpacing = mItemSpacingSeekBar.getProgress() * 2;

        mLayoutManagerController.setGridSpanCount(gridColumns);
        mLayoutManagerController.setItemSpacing(itemSpacing);

        if (selectedLayout != mLayoutManagerController.getCurrentLayoutType()) {
            mLayoutManagerController.switchLayout(selectedLayout, true);
        }

        mDialog.dismiss();
    }
}
