document.addEventListener('DOMContentLoaded', function () {
    loadSettings();
    document.getElementById('is-international').addEventListener('change', saveCheckboxSettings);
    document.getElementById('doesUserHaveEbaySubscription').addEventListener('change', saveCheckboxSettings);
    document.getElementById('isUserTaxExempt').addEventListener('change', saveCheckboxSettings);
});

function saveCheckboxSettings() {
    const isInternational = document.getElementById('is-international').checked;
    const doesUserHaveEbaySubscription = document.getElementById('doesUserHaveEbaySubscription').checked;
    const isUserTaxExempt = document.getElementById('isUserTaxExempt').checked;

    chrome.storage.local.set({ isInternational, doesUserHaveEbaySubscription, isUserTaxExempt }, function () {
        console.log('Setting updated');
    });
}

function loadSettings() {
    chrome.storage.local.get(
        { isInternational: false, doesUserHaveEbaySubscription: true, isUserTaxExempt: false },
        function (settings) {
            document.getElementById('is-international').checked = settings.isInternational;
            document.getElementById('doesUserHaveEbaySubscription').checked = settings.doesUserHaveEbaySubscription;
            document.getElementById('isUserTaxExempt').checked = settings.isUserTaxExempt;
        }
    );
}

// --- Force Item Location Switch ---
chrome.storage.local.get('forceItemLocationSwitch', function (result) {
    document.getElementById('forceItemLocationSwitch').checked = result.forceItemLocationSwitch ?? false;
    if (!result.forceItemLocationSwitch) {
        chrome.storage.local.set({ forceItemLocationSwitch: false });
    }
});
document.getElementById('forceItemLocationSwitch').addEventListener('change', function () {
    chrome.storage.local.set({ forceItemLocationSwitch: document.getElementById('forceItemLocationSwitch').checked });
});

// --- Item Location ---
chrome.storage.local.get('itemLocation', function (result) {
    document.getElementById('itemLocation').value = result.itemLocation ?? '';
    if (!result.itemLocation) {
        chrome.storage.local.set({ itemLocation: '' });
    }
});
document.getElementById('itemLocation').addEventListener('change', function () {
    chrome.storage.local.set({ itemLocation: document.getElementById('itemLocation').value });
});

// --- Item Location Country ---
chrome.storage.local.get('itemLocationCountry', function (result) {
    document.getElementById('itemLocationCountry').value = result.itemLocationCountry ?? '';
    if (!result.itemLocationCountry) {
        chrome.storage.local.set({ itemLocationCountry: '' });
    }
});
document.getElementById('itemLocationCountry').addEventListener('change', function () {
    chrome.storage.local.set({ itemLocationCountry: document.getElementById('itemLocationCountry').value });
});

// --- Item Location City/State ---
chrome.storage.local.get('itemLocationCityState', function (result) {
    document.getElementById('itemLocationCityState').value = result.itemLocationCityState ?? '';
    if (!result.itemLocationCityState) {
        chrome.storage.local.set({ itemLocationCityState: '' });
    }
});
document.getElementById('itemLocationCityState').addEventListener('change', function () {
    chrome.storage.local.set({ itemLocationCityState: document.getElementById('itemLocationCityState').value });
});

// --- Force Return Policy Switch ---
chrome.storage.local.get('forceReturnPolicySwitch', function (result) {
    document.getElementById('forceReturnPolicySwitch').checked = result.forceReturnPolicySwitch ?? false;
    if (!result.forceReturnPolicySwitch) {
        chrome.storage.local.set({ forceReturnPolicySwitch: false });
    }
});
document.getElementById('forceReturnPolicySwitch').addEventListener('change', function () {
    chrome.storage.local.set({ forceReturnPolicySwitch: document.getElementById('forceReturnPolicySwitch').checked });
});

// --- Return Policy ID ---
chrome.storage.local.get('returnPolicyId', function (result) {
    document.getElementById('returnPolicyId').value = result.returnPolicyId ?? '';
    if (!result.returnPolicyId) {
        chrome.storage.local.set({ returnPolicyId: '' });
    }
});
document.getElementById('returnPolicyId').addEventListener('change', function () {
    chrome.storage.local.set({ returnPolicyId: document.getElementById('returnPolicyId').value });
});

// --- Save Button ---
document.getElementById('save').addEventListener('click', function () {
    alert('Settings saved');
});

// --- Disable Thank You Message ---
chrome.storage.local.get('disableThankYouMessage', function (result) {
    document.getElementById('disableThankYouMessage').checked = result.disableThankYouMessage ?? false;
    if (!result.disableThankYouMessage) {
        chrome.storage.local.set({ disableThankYouMessage: false });
    }
});
document.getElementById('disableThankYouMessage').addEventListener('change', function () {
    chrome.storage.local.set({ disableThankYouMessage: document.getElementById('disableThankYouMessage').checked });
});

// --- Only List One Picture ---
chrome.storage.local.get('onlyListOnePicture', function (result) {
    document.getElementById('onlyListOnePicture').checked = result.onlyListOnePicture ?? false;
    if (!result.onlyListOnePicture) {
        chrome.storage.local.set({ onlyListOnePicture: false });
    }
});
document.getElementById('onlyListOnePicture').addEventListener('change', function () {
    chrome.storage.local.set({ onlyListOnePicture: document.getElementById('onlyListOnePicture').checked });
});

// --- Debug: Log all settings to console ---
async function logAllSettings() {
    const { itemLocation } = await chrome.storage.local.get('itemLocation');
    const { itemLocationCountry } = await chrome.storage.local.get('itemLocationCountry');
    const { itemLocationCityState } = await chrome.storage.local.get('itemLocationCityState');
    const { returnPolicyId } = await chrome.storage.local.get('returnPolicyId');
    const { disableThankYouMessage } = await chrome.storage.local.get('disableThankYouMessage');
    const { onlyListOnePicture } = await chrome.storage.local.get('onlyListOnePicture');

    console.log('itemLocation', itemLocation);
    console.log('itemLocationCountry', itemLocationCountry);
    console.log('itemLocationCityState', itemLocationCityState);
    console.log('returnPolicyId', returnPolicyId);
    console.log('disableThankYouMessage', disableThankYouMessage);
    console.log('onlyListOnePicture', onlyListOnePicture);
}
logAllSettings();



