/*
 * Copyright 2020 FormDev Software GmbH
 *
 * 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
 *
 *     https://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 com.formdev.flatlaf.testing.uidefaults;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Locale;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
import com.formdev.flatlaf.*;

/**
 * Collects all FlatLaf UI defaults keys and dumps them to a file.
 *
 * @author Karl Tauber
 */
public class UIDefaultsKeysDump
{
	public static void main( String[] args ) {
		Locale.setDefault( Locale.ENGLISH );
		System.setProperty( "sun.java2d.uiScale", "1x" );
		System.setProperty( FlatSystemProperties.UI_SCALE, "1x" );

		File keysFile = new File( "../flatlaf-theme-editor/src/main/resources/com/formdev/flatlaf/themeeditor/FlatLafUIKeys.txt" );

		// load existing keys file
		HashSet<String> keys = new HashSet<>();
		try( BufferedReader reader = new BufferedReader( new FileReader( keysFile ) ) ) {
			String key;
			while( (key = reader.readLine()) != null ) {
				keys.add( key );
			}
		} catch( IOException ex ) {
			ex.printStackTrace();
		}

		// collect keys used in Lafs
		collectKeys( FlatLightLaf.class.getName(), keys );
		collectKeys( FlatDarkLaf.class.getName(), keys );
		collectKeys( FlatIntelliJLaf.class.getName(), keys );
		collectKeys( FlatDarculaLaf.class.getName(), keys );

		// write key file
		try( Writer fileWriter = new BufferedWriter( new FileWriter( keysFile ) ) ) {
			String[] sortedKeys = keys.toArray( new String[keys.size()] );
			Arrays.sort( sortedKeys );
			for( String key : sortedKeys ) {
				fileWriter.write( key );
				fileWriter.write( "\n" );
			}
		} catch( IOException ex ) {
			ex.printStackTrace();
		}
	}

	private static void collectKeys( String lookAndFeelClassName, HashSet<String> keys ) {
		try {
			UIManager.setLookAndFeel( lookAndFeelClassName );
		} catch( Exception ex ) {
			ex.printStackTrace();
			return;
		}

		UIDefaults defaults = UIManager.getLookAndFeel().getDefaults();

		for( Object key : defaults.keySet() ) {
			if( key instanceof String )
				keys.add( (String) key );
		}
	}
}
