import os
import sys
from importlib.util import find_spec
from configparser import ConfigParser
import threading
import json
import time

import schedule
import requests

from flask import Flask, g, redirect, url_for, render_template, jsonify, request, send_from_directory
from flask_cors import CORS

from .web import api

from . import content_system as h_cs
from . import item_collections, view_model as h_vm
from .item_collections import item_collections_bp

from . import schedule_system as h_sched

theme_bootstrap5_enabled = False
if find_spec('theme_bootstrap5'):
    from theme_bootstrap5 import hogumathi_theme_bootstrap5_bp
    theme_bootstrap5_enabled = True
    
if find_spec('twitter_v2_facade'):
    import twitter_v2_facade
    from twitter_v2_facade import twitter_app as twitter_v2
    from twitter_v2_facade import oauth2_login
    twitter_enabled = True
    
else:
    print('twitter module not found.')
    twitter_enabled = False

if find_spec('twitter_v2_live_facade'):
    import twitter_v2_live_facade
    from twitter_v2_live_facade import twitter_app as twitter_v2_live
    
    
    import brands
    from brands import brands_bp
    
    twitter_live_enabled = True
else:
    print('twitter live module not found.')
    twitter_live_enabled = False

if find_spec('twitter_archive_facade'):
    import twitter_archive_facade
    from twitter_archive_facade import twitter_app as twitter_archive
    archive_enabled = True
else:
    print('twitter archive module not found.')
    archive_enabled = False

if find_spec('mastodon_facade'):
    from mastodon_facade import twitter_app as mastodon
    mastodon_enabled = True
else:
    print('mastodon module not found.')
    mastodon_enabled = False

if find_spec('feeds_facade'):
    import feeds_facade
    from feeds_facade import twitter_app as feeds
    feeds_enabled = True
else:
    print('feeds module not found.')
    feeds_enabled = False

if find_spec('youtube_v3_facade'):
    import youtube_v3_facade
    from youtube_v3_facade import youtube_app as youtube
    youtube_enabled = True
else:
    print('youtube module not found.')
    youtube_enabled = False

if find_spec('messages_facade'):
    from messages_facade import messages_facade as messages
    messages_enabled = True
else:
    print('messages module not found.')
    messages_enabled = False

if find_spec('instagram_embed_facade'):
    import instagram_embed_facade
    instagram_embed_enabled = True
else:
    print('instagram_embed module not found.')
    instagram_embed_enabled = False

if find_spec('instagram_facade'):
    import instagram_facade
    instagram_enabled = True
else:
    print('instagram module not found.')
    instagram_enabled = False

if find_spec('git_facade'):
    import git_facade
    git_enabled = True
else:
    print('git module not found.')
    git_enabled = False

if find_spec('bitchute_facade'):
    import bitchute_facade
    bitchute_enabled = True
else:
    print('bitchute module not found.')
    bitchute_enabled = False

if find_spec('videojs'):
    from videojs import videojs_bp
    videojs_enabled = True
else:
    print('videojs module not found.')
    videojs_enabled = False

if find_spec('visjs'):
    from visjs import visjs_bp
    visjs_enabled = True
else:
    print('messages module not found.')
    visjs_enabled = False


if find_spec('socialdata_facade'):
    import socialdata_facade
    socialdata_enabled = True
else:
    print('socialdata module not found.')
    socialdata_enabled = False

"""
By default we use embed because it's less likely to get banned, because it does not
use scraping. Once we've used scraping for a while and not been banned we can switch to that.

Biz note: scraping could also be an early access feature.
"""
print('disabling instagram_facade to avoid scraping... enable at your own risk')
instagram_enabled = False

#messages_enabled = False
#twitter_live_enabled = True

add_account_enabled = True


def record_content_analytics (content_id, content):
    if type(content) == h_vm.CollectionPage:
        prefix = None
        if content_id.startswith('twitter:feed:user:') or content_id.startswith('twitter:bookmarks:') or content_id.startswith('twitter:feed:reverse_chronological:user:'):
            prefix = 'twitter:tweet:'
        if not prefix:
            return;
            
        for item in content.items:
            h_cs.invoke_hooks('got_content', f'{prefix}{item.id}', item)
    elif type(content) == h_vm.FeedItem:
        print(f'record_content_analytics: feed item: {content_id}')
    elif type(content) == h_vm.FeedServiceUser:
        print(f'record_content_analytics: user: {content_id}')
    else:
        print(f'record_content_analytics: unknown type: {content_id}')

def browser_native_messaging ():
    # stdin = binary stream. ((payload length, payload), ...)
    
    print('hey. awaiting msg.')
    
    # "Note To write or read binary data from/to the standard streams, 
    # use the underlying binary buffer object. For example, to write bytes to stdout, use sys.stdout.buffer.write(b'abc')."
    # https://docs.python.org/3/library/sys.html#sys.stdin
    
    stdin = sys.stdin
    
    msg_len = read_int(stdin.buffer) # example read_int is in Apple Notes exporter code
    while msg_len:
        
        print(f'new msg_len: {msg_len}')
        
        msg_bytes = stdin.buffer.read(msg_len)
        msg = json.loads(msg_bytes.decode('utf-8'))
        
        print(msg)
        
        print('awaiting next msg...')
        msg_len = read_int(stdin.buffer)
        
        return 0

if __name__ == '__main__':
    if len(sys.argv) >= 2 and 	sys.argv[1] == 'browser_native_messaging':
        print('browser_native_messaging')
        
        exit(browser_native_messaging())
        
    
    glitch_enabled = os.environ.get('PROJECT_DOMAIN') and True
    
    t = os.environ.get('BEARER_TOKEN')

    PORT = int(os.environ.get('PORT', 5000))
    HOST = os.environ.get('HOST', '127.0.0.1')
    
    archive_enabled = os.environ.get('ARCHIVE_TWEETS_PATH') and True
    
    
    notes_app_url = os.environ.get('NOTES_APP_URL')
    bedrss_app_url = os.environ.get('NOTES_APP_URL')
    
    plots_app_url = os.environ.get('PLOTS_APP_URL')
    videos_app_url = os.environ.get('VIDEOS_APP_URL')
    messages_app_url = os.environ.get('MESSAGES_APP_URL')
    
    if not os.path.exists('.data'):
        os.mkdir('.data')
    
    if not os.path.exists('.data/cache'):
        os.mkdir('.data/cache')
    
    
    
    
    # HACK - environ from .env isn't set yet when the import happens. We should call an init function somewhere.
    oauth2_login.app_access_token = os.environ.get("BEARER_TOKEN")
    oauth2_login.app_consumer_key = os.environ.get("TWITTER_CONSUMER_KEY")
    oauth2_login.app_secret_key = os.environ.get("TWITTER_CONSUMER_SECRET")
    
    h_cs.register_hook('got_content', record_content_analytics)
    
    @api.before_request
    def add_config ():
        g.twitter_enabled = twitter_enabled
        g.archive_enabled = archive_enabled
        g.mastodon_enabled = mastodon_enabled
        g.feeds_enabled = feeds_enabled
        g.youtube_enabled = youtube_enabled
        g.messages_enabled = messages_enabled
        g.twitter_live_enabled = twitter_live_enabled
        
        g.add_account_enabled = add_account_enabled
        
        
        g.glitch_enabled = glitch_enabled
        
        g.videojs_enabled = videojs_enabled
        g.visjs_enabled = visjs_enabled
        
        if glitch_enabled:
            g.app_url = 'https://{}.glitch.me'.format( os.environ.get('PROJECT_DOMAIN') )
        else:
            g.app_url = 'http://{}:{}'.format('localhost', PORT)
            
        if notes_app_url:
            g.notes_app_url = notes_app_url
        if bedrss_app_url:
            g.bedrss_app_url = bedrss_app_url
        if plots_app_url:
            g.plots_app_url = plots_app_url
        if videos_app_url:
            g.videos_app_url = videos_app_url
        if messages_app_url:
            g.messages_app_url = messages_app_url
            
         
            
        if theme_bootstrap5_enabled:
            g.theme_bootstrap5_enabled = theme_bootstrap5_enabled
    
    
    @api.context_processor
    def inject_config ():
        config = {}
        config['twitter_enabled'] = twitter_enabled
        config['archive_enabled'] = archive_enabled
        config['mastodon_enabled'] = mastodon_enabled
        config['feeds_enabled'] = feeds_enabled
        config['youtube_enabled'] = youtube_enabled
        config['messages_enabled'] = messages_enabled
        config['twitter_live_enabled'] = twitter_live_enabled
        
        config['add_account_enabled'] = add_account_enabled
        
        config['glitch_enabled'] = glitch_enabled
        
        config['videojs_enabled'] = videojs_enabled
        config['visjs_enabled'] = visjs_enabled
        
        config['theme_bootstrap5_enabled'] = theme_bootstrap5_enabled

        if notes_app_url:
            config['notes_app_url'] = notes_app_url
        if bedrss_app_url:
            config['bedrss_app_url'] = bedrss_app_url
        
        if plots_app_url:
            config['plots_app_url'] = plots_app_url
        if videos_app_url:
            config['videos_app_url'] = videos_app_url
        if messages_app_url:
            config['messages_app_url'] = messages_app_url
            
        
        return config
    
    
    
    api.secret_key = os.environ.get('FLASK_SECRET')
    
    api.config['TEMPLATES_AUTO_RELOAD'] = True
    
    # the new way
    #api.config['notes_app_url'] = notes_app_url
    
    if theme_bootstrap5_enabled:
        api.register_blueprint(hogumathi_theme_bootstrap5_bp)
    
    if videojs_enabled:
        api.register_blueprint(videojs_bp, url_prefix='/lib/videojs')
        
    if visjs_enabled:
        api.register_blueprint(visjs_bp, url_prefix='/lib/visjs')

    twitter_v2_facade.register_content_sources()
    api.register_blueprint(twitter_v2, url_prefix='/twitter')
    
    if archive_enabled:
        twitter_archive_facade.register_content_sources()
        api.register_blueprint(twitter_archive, url_prefix='/twitter-archive')
    
    if mastodon_enabled:
        api.register_blueprint(mastodon, url_prefix='/mastodon')
        

    if youtube_enabled:
        youtube_v3_facade.register_content_sources()
        api.register_blueprint(youtube, url_prefix='/youtube')
    
    if messages_enabled:
        api.register_blueprint(messages, url_prefix='/messages')
        
    
    item_collections.register_content_sources()
    api.register_blueprint(item_collections_bp, url_prefix='/collections')
    
    if twitter_live_enabled:
        twitter_v2_live_facade.register_content_sources()
        api.register_blueprint(twitter_v2_live, url_prefix='/twitter-live')
        
        brands.register_content_sources()
        api.register_blueprint(brands_bp, url_prefix='/')
    
    if instagram_enabled:
        instagram_facade.register_content_sources()
    
    if instagram_embed_enabled:
        instagram_embed_facade.register_content_sources()
    
    if feeds_enabled:
        if not os.path.exists('.data/cache/feeds'):
            os.mkdir('.data/cache/feeds')
        
        feeds_facade.register_content_sources()
        api.register_blueprint(feeds, url_prefix='/feeds')
    
    if git_enabled:
        git_facade.register_content_sources()
        
    if bitchute_enabled:
        bitchute_facade.register_content_sources()
    
    if socialdata_enabled:
        socialdata_facade.register_content_sources()
    
    #CORS(api)
    
    sched_app = h_sched.ScheduleApplication()
    print(f'created schedule app: {sched_app}')
    
    running = True
    def schedule_main():
        
        print(f'running schedule app: {sched_app}')
        
        while running:
            sched_app.run_pending()
            time.sleep(1)
    
    def say_something ():
        print ('-- something')
    
    sched_app.schedule_job(schedule.every(61).seconds, say_something)
    
    schedule_thread = threading.Thread(target=schedule_main)
    schedule_thread.start()
    
    api.run(port=PORT, host=HOST)
    
    running = False
    schedule_thread.join()