/*
 * SDK Pullenti Unitext, version 4.33, fabruary 2026. Copyright (c) 2013-2026, Pullenti. All rights reserved. 
 * Non-Commercial Freeware and Commercial Software. 
 * This class is generated using the converter Unisharping (www.unisharping.ru) from Pullenti C# project. 
 * The latest version of the code is available on the site www.pullenti.ru
 */

package com.pullenti.unitext.internal.zip;

// Basic implementation of <see cref="IEntryFactory"></see>
public class ZipEntryFactory implements IEntryFactory {

    public ZipEntryFactory(TimeSetting timeSetting) {
        if(_globalInstance == null) return;
        timeSetting_ = timeSetting;
        nameTransform_ = new ZipNameTransform(null);
    }

    @Override
    public INameTransform getNameTransform() {
        return nameTransform_;
    }

    @Override
    public INameTransform setNameTransform(INameTransform value) {
        if (value == null) 
            nameTransform_ = new ZipNameTransform(null);
        else 
            nameTransform_ = value;
        return value;
    }


    public TimeSetting getSetting() {
        return timeSetting_;
    }

    public TimeSetting setSetting(TimeSetting value) {
        timeSetting_ = value;
        return value;
    }


    public java.time.LocalDateTime getFixedDateTime() {
        return fixedDateTime_;
    }

    public java.time.LocalDateTime setFixedDateTime(java.time.LocalDateTime value) throws IllegalArgumentException {
        if (value.getYear() < 1970) 
            throw new IllegalArgumentException("Value is too old to be valid" + " " + "value");
        fixedDateTime_ = value;
        return value;
    }


    public int getGetAttributes() {
        return getAttributes_;
    }

    public int setGetAttributes(int value) {
        getAttributes_ = value;
        return value;
    }


    public int getSetAttributes() {
        return setAttributes_;
    }

    public int setSetAttributes(int value) {
        setAttributes_ = value;
        return value;
    }


    public boolean isUnicodeText() {
        return isUnicodeText_;
    }

    public boolean setUnicodeText(boolean value) {
        isUnicodeText_ = value;
        return value;
    }


    @Override
    public ZipEntry makeFileEntry(String fileName) {
        return this.makeFileEntryEx(fileName, true);
    }

    @Override
    public ZipEntry makeFileEntryEx(String fileName, boolean useFileSystem) {
        try {
            ZipEntry result = new ZipEntry(nameTransform_.transformFile(fileName), 0, ZipConstants.VERSIONMADEBY, CompressionMethod.DEFLATED);
            result.setUnicodeText(isUnicodeText_);
            int externalAttributes = 0;
            boolean useAttributes = setAttributes_ != 0;
            java.io.File fi = null;
            if (useFileSystem) 
                fi = new java.io.File(fileName);
            if ((fi != null) && fi.exists()) {
                result.setSize((int)fi.length());
                useAttributes = true;
            }
            else if (timeSetting_ == TimeSetting.FIXED) 
                result.setDateTime(fixedDateTime_);
            if (useAttributes) {
                externalAttributes |= setAttributes_;
                result.setExternalFileAttributes(externalAttributes);
            }
            return result;
        } catch (Exception ex123) {
            return null;
        }
    }

    @Override
    public ZipEntry makeDirectoryEntry(String directoryName) {
        return this.makeDirectoryEntryEx(directoryName, true);
    }

    @Override
    public ZipEntry makeDirectoryEntryEx(String directoryName, boolean useFileSystem) {
        try {
            ZipEntry result = new ZipEntry(nameTransform_.transformDirectory(directoryName), 0, ZipConstants.VERSIONMADEBY, CompressionMethod.DEFLATED);
            result.setUnicodeText(isUnicodeText_);
            result.setSize(0);
            int externalAttributes = 0;
            java.io.File di = null;
            if (useFileSystem) 
                di = new java.io.File(directoryName);
            externalAttributes |= ((setAttributes_ | 16));
            result.setExternalFileAttributes(externalAttributes);
            return result;
        } catch (Exception ex123) {
            return null;
        }
    }

    private INameTransform nameTransform_;

    private java.time.LocalDateTime fixedDateTime_ = java.time.LocalDateTime.now();

    private TimeSetting timeSetting_ = TimeSetting.LASTWRITETIME;

    private boolean isUnicodeText_ = true;

    private int getAttributes_ = -1;

    private int setAttributes_;

    /**
     * Defines the possible values to be used for the <see cref="ZipEntry.DateTime"/>.
     */
    public static class TimeSetting implements Comparable<TimeSetting> {
    
        /**
         * Use the recorded LastWriteTime value for the file.
         */
        public static final TimeSetting LASTWRITETIME; // 0
    
        /**
         * Use the recorded LastWriteTimeUtc value for the file
         */
        public static final TimeSetting LASTWRITETIMEUTC; // 1
    
        /**
         * Use the recorded CreateTime value for the file.
         */
        public static final TimeSetting CREATETIME; // 2
    
        /**
         * Use the recorded CreateTimeUtc value for the file.
         */
        public static final TimeSetting CREATETIMEUTC; // 3
    
        /**
         * Use the recorded LastAccessTime value for the file.
         */
        public static final TimeSetting LASTACCESSTIME; // 4
    
        /**
         * Use the recorded LastAccessTimeUtc value for the file.
         */
        public static final TimeSetting LASTACCESSTIMEUTC; // 5
    
        /**
         * Use a fixed value.
         */
        public static final TimeSetting FIXED; // 6
    
    
        public int value() { return m_val; }
        private int m_val;
        private String m_str;
        private TimeSetting(int val, String str) { m_val = val; m_str = str; }
        @Override
        public String toString() {
            if(m_str != null) return m_str;
            return ((Integer)m_val).toString();
        }
        @Override
        public int hashCode() {
            return (int)m_val;
        }
        @Override
        public int compareTo(TimeSetting v) {
            if(m_val < v.m_val) return -1;
            if(m_val > v.m_val) return 1;
            return 0;
        }
        private static java.util.HashMap<Integer, TimeSetting> mapIntToEnum; 
        private static java.util.HashMap<String, TimeSetting> mapStringToEnum; 
        private static TimeSetting[] m_Values; 
        private static java.util.Collection<Integer> m_Keys; 
        public static TimeSetting of(int val) {
            if (mapIntToEnum.containsKey(val)) return mapIntToEnum.get(val);
            TimeSetting item = new TimeSetting(val, ((Integer)val).toString());
            mapIntToEnum.put(val, item);
            mapStringToEnum.put(item.m_str.toUpperCase(), item);
            return item; 
        }
        public static TimeSetting of(String str) {
            str = str.toUpperCase(); 
            if (mapStringToEnum.containsKey(str)) return mapStringToEnum.get(str);
            return null; 
        }
        public static boolean isDefined(Object val) {
            if(val instanceof Integer) return m_Keys.contains((Integer)val); 
            return false; 
        }
        public static TimeSetting[] getValues() {
            return m_Values; 
        }
        static {
            mapIntToEnum = new java.util.HashMap<Integer, TimeSetting>();
            mapStringToEnum = new java.util.HashMap<String, TimeSetting>();
            LASTWRITETIME = new TimeSetting(0, "LASTWRITETIME");
            mapIntToEnum.put(LASTWRITETIME.value(), LASTWRITETIME);
            mapStringToEnum.put(LASTWRITETIME.m_str.toUpperCase(), LASTWRITETIME);
            LASTWRITETIMEUTC = new TimeSetting(1, "LASTWRITETIMEUTC");
            mapIntToEnum.put(LASTWRITETIMEUTC.value(), LASTWRITETIMEUTC);
            mapStringToEnum.put(LASTWRITETIMEUTC.m_str.toUpperCase(), LASTWRITETIMEUTC);
            CREATETIME = new TimeSetting(2, "CREATETIME");
            mapIntToEnum.put(CREATETIME.value(), CREATETIME);
            mapStringToEnum.put(CREATETIME.m_str.toUpperCase(), CREATETIME);
            CREATETIMEUTC = new TimeSetting(3, "CREATETIMEUTC");
            mapIntToEnum.put(CREATETIMEUTC.value(), CREATETIMEUTC);
            mapStringToEnum.put(CREATETIMEUTC.m_str.toUpperCase(), CREATETIMEUTC);
            LASTACCESSTIME = new TimeSetting(4, "LASTACCESSTIME");
            mapIntToEnum.put(LASTACCESSTIME.value(), LASTACCESSTIME);
            mapStringToEnum.put(LASTACCESSTIME.m_str.toUpperCase(), LASTACCESSTIME);
            LASTACCESSTIMEUTC = new TimeSetting(5, "LASTACCESSTIMEUTC");
            mapIntToEnum.put(LASTACCESSTIMEUTC.value(), LASTACCESSTIMEUTC);
            mapStringToEnum.put(LASTACCESSTIMEUTC.m_str.toUpperCase(), LASTACCESSTIMEUTC);
            FIXED = new TimeSetting(6, "FIXED");
            mapIntToEnum.put(FIXED.value(), FIXED);
            mapStringToEnum.put(FIXED.m_str.toUpperCase(), FIXED);
            java.util.Collection<TimeSetting> col = mapIntToEnum.values();
            m_Values = new TimeSetting[col.size()];
            col.toArray(m_Values);
            m_Keys = new java.util.ArrayList<Integer>(mapIntToEnum.keySet());
        }
    }

    public ZipEntryFactory() {
        this(TimeSetting.LASTWRITETIME);
    }
    public static ZipEntryFactory _globalInstance;
    
    static {
        try { _globalInstance = new ZipEntryFactory(); } 
        catch(Exception e) { }
    }
}
