from django.db import models


from django.core.files.storage import FileSystemStorage
from django.conf import settings
import os

# class OverwriteStorage(FileSystemStorage):
#
#     def get_available_name(self, name, max_length=None):
#         """Returns a filename that's free on the target storage system, and
#         available for new content to be written to.
#
#         Found at http://djangosnippets.org/snippets/976/
#
#         This file storage solves overwrite on upload problem. Another
#         proposed solution was to override the save method on the model
#         like so (from https://code.djangoproject.com/ticket/11663):
#
#         def save(self, *args, **kwargs):
#             try:
#                 this = MyModelName.objects.get(id=self.id)
#                 if this.MyImageFieldName != self.MyImageFieldName:
#                     this.MyImageFieldName.delete()
#             except: pass
#             super(MyModelName, self).save(*args, **kwargs)
#         """
#         # If the filename already exists, remove it as if it was a true file system
#         from os import listdir
#         from os.path import isfile, join
#         files = [f for f in listdir(settings.MEDIA_ROOT) if isfile(join(settings.MEDIA_ROOT, f))]
#         print("dd", self.values)
#         print(name)
#         if self.exists(name):
#             os.remove(os.path.join(settings.MEDIA_ROOT, name))
#         return name


class SocialLink(models.Model):
    linkedin = models.CharField(max_length=100, blank=True, null=True)
    twitter = models.CharField(max_length=100, blank=True, null=True)
    facebook = models.CharField(max_length=100, blank=True, null=True)
    instagram = models.CharField(max_length=100, blank=True, null=True)


class Game(models.Model):
    order = models.IntegerField(blank=True, null=True)
    name = models.CharField(max_length=50, blank=True, null=True)
    description = models.TextField(max_length=1000, blank=True, null=True)
    logo = models.FileField(blank=True, null=True)
    image1 = models.FileField(blank=True, null=True)
    image2 = models.FileField(blank=True, null=True)
    image3 = models.FileField(blank=True, null=True)


class Partner(models.Model):
    order = models.IntegerField(blank=True, null=True)
    name = models.CharField(max_length=50, blank=True, null=True)
    logo = models.FileField(blank=True, null=True) #, storage=OverwriteStorage())


class ContactInfo(models.Model):
    appstore = models.CharField(max_length=100, blank=True, null=True)
    playstore = models.CharField(max_length=100, blank=True, null=True)
    email = models.CharField(max_length=100, blank=True, null=True)
    location = models.CharField(max_length=100, blank=True, null=True)

class Policy(models.Model):
    content = models.TextField(blank=True, null=True)