/*
 * 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.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import net.micode.notes.R;
import net.micode.notes.tool.ThemeManager;

public class ThemeSettingsActivity extends Activity {
    private LinearLayout mLightThemeLayout;
    private LinearLayout mDarkThemeLayout;
    private LinearLayout mSystemThemeLayout;
    private TextView mCurrentThemeText;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ThemeManager.applyTheme(this);
        setContentView(R.layout.theme_settings);
        
        initViews();
        updateCurrentTheme();
    }
    
    private void initViews() {
        mLightThemeLayout = findViewById(R.id.light_theme_layout);
        mDarkThemeLayout = findViewById(R.id.dark_theme_layout);
        mSystemThemeLayout = findViewById(R.id.system_theme_layout);
        mCurrentThemeText = findViewById(R.id.current_theme_text);
        
        mLightThemeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setAppTheme(ThemeManager.THEME_LIGHT);
            }
        });

        mDarkThemeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setAppTheme(ThemeManager.THEME_DARK);
            }
        });

        mSystemThemeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setAppTheme(ThemeManager.THEME_SYSTEM);
            }
        });
    }
    
    private void setAppTheme(final int themeMode) {
        int currentTheme = ThemeManager.getThemeMode(this);
        if (currentTheme == themeMode) {
            return;
        }
        
        new AlertDialog.Builder(this)
            .setTitle(R.string.theme_settings)
            .setMessage("Change theme to " + ThemeManager.getThemeName(this, themeMode) + "?")
            .setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    ThemeManager.setThemeMode(ThemeSettingsActivity.this, themeMode);
                    
                    // Restart the app to apply theme
                    Intent intent = new Intent(ThemeSettingsActivity.this, NotesListActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                    finish();
                }
            })
            .setNegativeButton(R.string.cancel, null)
            .show();
    }
    
    private void updateCurrentTheme() {
        int currentTheme = ThemeManager.getThemeMode(this);
        String themeName = ThemeManager.getThemeName(this, currentTheme);
        mCurrentThemeText.setText("Current: " + themeName);
        
        // Update selection indicators
        mLightThemeLayout.setSelected(currentTheme == ThemeManager.THEME_LIGHT);
        mDarkThemeLayout.setSelected(currentTheme == ThemeManager.THEME_DARK);
        mSystemThemeLayout.setSelected(currentTheme == ThemeManager.THEME_SYSTEM);
    }
}
