import requests
import json
from decouple import config

API_KEY = config('API_KEY', default="35a15b812a541ac37eeea476dc446242")
SHARED_SECRET = config('SHARED_SECRET', default="2551e32b3f42e0a0d16a29788fb23809")
API_VERSION = config('API_VERSION', default="2023-01")


class Assets_Api:
    @staticmethod
    def get_theme(shop, token):
        get_data = ''

        url = "https://%s/admin/api/%s/themes.json"%(shop, API_VERSION)

        header = {
            'X-Shopify-Access-Token': token
        }
        
        r=requests.get(url=url,headers=header)
        
        if r.status_code == 200:
            get_data = r.text
        else:
            get_data = {
                "error":"API Not Called"
            }

        return json.loads(get_data)

    @staticmethod
    def set_css(theme_id, shop, token):
        url = 'https://%s:%s@%s/admin/api/%s/themes/%s/assets.json' % (API_KEY, SHARED_SECRET, shop, API_VERSION, theme_id)

        header = {
            'X-Shopify-Access-Token': token,
            'Content-Type': 'application/json',
            'client_id': API_KEY,
            'client_secret': SHARED_SECRET
        } 

        data = {
            'asset': {
                'key': 'assets/sleepezee.css',
                'src': 'https://%s/static/project/frontend.css'%(config("tunnel", default='dev.sleepezee.com'))
            }
        }
        
        r = requests.put(url=url, headers=header, json=data)
        print(r.status_code, "css")
        print(r.text)
        if r.status_code == 200:
            c = json.loads(r.text)
            return c
        else:
            c = ''
            return c
        
    @staticmethod
    def set_js(theme_id, shop, token):
        url = 'https://%s:%s@%s/admin/api/%s/themes/%s/assets.json' % (API_KEY, SHARED_SECRET, shop, API_VERSION, theme_id)

        header = {
            'X-Shopify-Access-Token': token,
            'Content-Type': 'application/json',
            'client_id': API_KEY,
            'client_secret': SHARED_SECRET
        } 

        data = {
            'asset': {
                'key': 'assets/sleepezee.js',
                'src': 'https://%s/static/project/frontend.js'%(config("tunnel", default='dev.sleepezee.com'))
            }
        }
        
        r = requests.put(url=url, headers=header, json=data)
        print(r.status_code, "js")
        print(r.text)
        if r.status_code == 200:
            c = json.loads(r.text)
            return c
        else:
            c = ''
            return c
        
    @staticmethod
    def set_liquid(theme_id, shop, token):
        url = 'https://%s:%s@%s/admin/api/%s/themes/%s/assets.json' % (API_KEY, SHARED_SECRET, shop, API_VERSION, theme_id)

        header = {
            'X-Shopify-Access-Token': token,
            'Content-Type': 'application/json',
            'client_id': API_KEY,
            'client_secret': SHARED_SECRET
        } 

        data = {
            'asset': {
                'key': 'sections/sleepezee.liquid',
                'value': '<label for="name">Name:</label><input id="name" name="name" required pattern="[a-zA-Z\s]+"><label for="contactNumber">Contact Number:</label><input id="contactNumber" name="contactNumber" required pattern="[0-9]+"><label for="city">City:</label><input id="city" name="city" required pattern="[a-zA-Z\s]+"><label for="email">Email:</label><input type="email" id="email" name="email" required><label for="gender">Gender:</label><select id="gender" name="gender" required><option value="">Select</option><option value="male">Male</option><option value="female">Female</option><option value="other">Other</option></select><label for="age">Age:</label><input type="number" id="age" name="age" required><label for="height">Height:</label><input id="height" name="height" required><label for="weight">Weight (in kg):</label><input type="number" id="weight" name="weight" required><label for="getting_out_of_bed">Getting Out Of Bed:</label><select id="getting_out_of_bed" name="getting_out_of_bed" required><option value="">Select</option><option value="Some difficulty">Some Difficulty</option><option value="No difficulty">No Difficulty</option><option value="Very difficult">Very Difficult</option><option value="Completely unable to do">Completely unable to do</option></select><div><label for="driving_and_sitting_in_a_car_or_bus">Driving and sitting in a car or bus:</label><select id="driving_and_sitting_in_a_car_or_bus" name="driving_and_sitting_in_a_car_or_bus"><option value="">Select</option><option value="Some difficulty">Some difficulty</option><option value="No difficulty">No difficulty</option><option value="Very difficult">Very difficult</option><option value="Completely unable to do">Completely unable to do</option></select></div><div><label for="climbing_15_to_20_stairs">Climbing 15 to 20 stairs:</label><select id="climbing_15_to_20_stairs" name="climbing_15_to_20_stairs"><option value="">Select</option><option value="Some difficulty">Some difficulty</option><option value="No difficulty">No difficulty</option><option value="Very difficult">Very difficult</option><option value="Completely unable to do">Completely unable to do</option></select></div><div><label for="lifting_and_carrying_bag_luggage">Lifting and carrying bag/luggage:</label><select id="lifting_and_carrying_bag_luggage" name="lifting_and_carrying_bag_luggage"><option value="">Select</option><option value="Some difficulty">Some difficulty</option><option value="No difficulty">No difficulty</option><option value="Very difficult">Very difficult</option><option value="Completely unable to do">Completely unable to do</option></select></div><input type="submit" value="Submit">'
            }
        }
        
        r = requests.put(url=url, headers=header, json=data)
        print(r.status_code, "liquid")
        print(r.text)
        if r.status_code == 200:
            c = json.loads(r.text)
            return c
        else:
            c = ''
            return c

    @staticmethod
    def get_email(shop, token):
        get_data = {}

        url = "https://%s/admin/api/%s/shop.json"%(shop, API_VERSION)

        header = {
            'X-Shopify-Access-Token': token
        }
        
        r=requests.get(url=url,headers=header)
        
        if r.status_code == 200:
            data = json.loads(r.text)
            get_data["owner_email"] = data["shop"]["email"]
        else:
            get_data["error"] = "API Not Called"
            
        return get_data
