import {
    CmsCore, CmsWebsitesApi,
    ContentBlock,
    ContentBlockType,
    Flex,
    MainColors,
    Page,
    Website, WebsiteAndId
} from "skysail-cms-core";

export var cmsCore2:CmsCore = null as unknown as CmsCore

// export const initializeCmsUi2 = (url: string, key: string, domain: string) => {
//     cmsCore2 = initializeCmsCore(url, key, domain);
// }

// @ts-ignore
//initializeCmsUi2(process.env.backendUrl,process.env.firebaseApiKey,process.env.firebaseAuthDomain)
//console.log("cms core set to ", cmsCore2)

export function initializeCmsUi(
    backendUrl: string,
    apiKey: string,
    authDomain: string
): CmsCore {
    console.log("====================================================")
    console.log("=== initializing cmsCore in cmsUi with url", backendUrl)
    console.log("====================================================")
    cmsCore2 = new CmsCore(backendUrl, apiKey, authDomain)
    return cmsCore2
}

class MockCmsWebsitesApi extends CmsWebsitesApi {

    websiteWithId = null as unknown as WebsiteAndId

    constructor() {
        super("mock", "mock");

        const websiteOne = new Website("websiteOneId", "websiteOne", "", "","title",
            new Flex("","","","","",""),
            null as unknown as MainColors,
            null as unknown as MainColors)
        const elements: ContentBlock[] = []
        elements.push(new ContentBlock("theId2", ContentBlockType.ContentBlockHeading, "content"))
        const pageOne = new Page("pageOneId", "namePageOne", 1, 0,0,0,elements)
        const pages: Page[] = []
        pages.push(pageOne)
        websiteOne.setCurrentPages(pages)
        websiteOne.setOriginalPages(pages)
        this.websiteWithId = new WebsiteAndId("websiteOneId", websiteOne)

    }

    getWebsites(): Promise<any> {
        console.log("mocking getWebsites call with", this.websiteWithId)
        return Promise.resolve({data: this.websiteWithId});
    }

    getWebsite(websiteId: string): Promise<any> {
        console.log("mocking getWebsites(id) call with", this.websiteWithId)
        return Promise.resolve(
            {
                data: {
                    "originalPages": [
                        {
                            "published": 1651300078007,
                            "dirty": false,
                            "created": 1647156111671,
                            "elements": [
                                {
                                    "type": "CmsHeading",
                                    "content": "content editable",
                                    "id": "heading2Id",
                                    "attributes": {
                                        "h": "5"
                                    }
                                },
                                {
                                    "attributes": {
                                        "h": 2
                                    },
                                    "element": null,
                                    "elements": [],
                                    "type": "CmsHeading",
                                    "content": "new CmsHeading!!",
                                    "id": "45e31289-bb9d-46e5-b9a8-46a8db566ba0"
                                },
                                {
                                    "type": "CmsParagraph",
                                    "attributes": {},
                                    "id": "db51ce3a-6dfe-40b1-95bd-cb90717d09c5",
                                    "content": "new CmsParagraph",
                                    "element": null,
                                    "elements": []
                                }
                            ],
                            "version": 55,
                            "name": "index",
                            "changed": 1650963589322,
                            "id": "ec69fb8f-f6ba-4501-9852-f1455b3e390f"
                        }
                    ],
                    "currentPages": [
                        {
                            "published": 1651300078007,
                            "name": "index",
                            "version": 63,
                            "created": 1647156111671,
                            "dirty": false,
                            "elements": [
                                {
                                    "content": "first heading for test 1647156108397",
                                    "attributes": {
                                        "h": "3"
                                    },
                                    "id": "heading2Id",
                                    "type": "CmsHeading"
                                },
                                {
                                    "content": "first heading for test 1647156108397",
                                    "attributes": {
                                        "h": "3"
                                    },
                                    "id": "heading3Id",
                                    "type": "CmsHeading"
                                },
                                {
                                    "elements": [],
                                    "element": null,
                                    "type": "CmsText",
                                    "id": "text2Id",
                                    "content": "content editable"
                                },
                                {
                                    "elements": [],
                                    "element": null,
                                    "id": "ec9c568b-7df0-4916-bd88-db4f72f8dbda",
                                    "attributes": {},
                                    "content": "new undefined"
                                },
                                {
                                    "element": null,
                                    "elements": [],
                                    "attributes": {
                                        "columns": [
                                            {
                                                "type": "string",
                                                "name": "type"
                                            },
                                            {
                                                "type": "string",
                                                "name": "name"
                                            },
                                            {
                                                "type": "string",
                                                "name": "label"
                                            },
                                            {
                                                "type": "string",
                                                "name": "category"
                                            },
                                            {
                                                "type": "object",
                                                "name": "tags"
                                            },
                                            {
                                                "type": "object",
                                                "name": "groupNames"
                                            },
                                            {
                                                "type": "string",
                                                "name": "link"
                                            },
                                            {
                                                "name": "state",
                                                "type": "string"
                                            },
                                            {
                                                "name": "transformedState",
                                                "type": "undefined"
                                            },
                                            {
                                                "name": "stateDescription",
                                                "type": "unknown"
                                            },
                                            {
                                                "type": "unknown",
                                                "name": "commandDescription"
                                            },
                                            {
                                                "type": "undefined",
                                                "name": "metadata"
                                            },
                                            {
                                                "name": "editable",
                                                "type": "boolean"
                                            }
                                        ]
                                    },
                                    "type": "CmsTable",
                                    "content": "new CmsTable",
                                    "id": "f3fa98fd-e317-4b97-8e3f-1a1df2917d6e"
                                },
                                {
                                    "content": "new CmsHeading!!",
                                    "elements": [],
                                    "type": "CmsHeading",
                                    "attributes": {
                                        "color": "accent",
                                        "h": 2
                                    },
                                    "element": null,
                                    "id": "c08d95dd-fe92-4e60-82e2-e33f07cc6a32"
                                },
                                {
                                    "type": "CmsParagraph",
                                    "elements": [],
                                    "element": null,
                                    "attributes": {},
                                    "content": "new CmsParagraph",
                                    "id": "db51ce3a-6dfe-40b1-95bd-cb90717d09c5"
                                }
                            ],
                            "id": "ec69fb8f-f6ba-4501-9852-f1455b3e390f",
                            "changed": 1652164740171
                        }
                    ],
                    "flex": {
                        "draft": {
                            "direction": "column"
                        },
                        "original": {
                            "direction": "column"
                        }
                    },
                    "nav": [],
                    "bgColor": "#ffffff",
                    "fontColor": "#000000",
                    "bgImg": null
                }
            }
        );
    }
}

export function initializeMockCmsUi(): CmsCore {
    console.log("==========================================")
    console.log("=== initializing cmsCore Mock in cmsUi ===")
    console.log("==========================================")
    cmsCore2 = new CmsCore("mock", "mock", "mock")

    // const websiteOne = new Website("websiteOne", "websiteOneName", "","","title",
    //     null as unknown as Flex, null as unknown as MainColors, null as unknown as MainColors)
    // const elements: ContentBlock[] = []
    // elements.push(new ContentBlock("theId2", ContentBlockType.ContentBlockHeading, "content"))
    // const pageOne = new Page("pageOneId", "namePageOne", 1, 0,0,0,elements)
    // const pages: Page[] = []
    // pages.push(pageOne)
    // websiteOne.setCurrentPages(pages)
    // websiteOne.setOriginalPages(pages)
    // cmsCore2.websitesStore.setCurrentWebsite(websiteOne)
    const mockCmsWebsitesApi = new MockCmsWebsitesApi()
    cmsCore2.cmsWebsitesApi = mockCmsWebsitesApi
    cmsCore2.websitesStore.cmsWebsitesApi = mockCmsWebsitesApi
    const websites: Website[] = []
    websites.push(mockCmsWebsitesApi.websiteWithId as unknown as Website)
    cmsCore2.websitesStore.websites = websites
    cmsCore2.websitesStore.setCurrentWebsiteById("websiteOneId")

    return cmsCore2
}

export default {
    cmsCore2,
    initializeCmsUi
}

//module.exports = {initializeCmsUi2,cmsCore2}
