import os
import pygame
import pytmx
from pygame.constants import QUIT
from pygame.locals import *

import utils.tiled_render as tiled
from actor.earth_god import EarthGod
from actor.elder import Elder
from actor.elder2 import Elder2
from actor.elder4 import Elder4
from actor.swk import swk
from scene import SceneResult
from scene.fade_scene import FadeScene, SceneStatus
from scene.tiled_scene import TileScene as tilescene
import actor

class VillageScene:
    scene_result = SceneResult.Ongoing
    def __init__(self, swk , NAME_1:pygame.NAME_1):
        self.screen = NAME_1
        backtmxpath = os.path.join("./resource","tmx","village1.tmx")
        self.tiled_scene = tilescene(backtmxpath)
        self.back_NAME_1 = self.tiled_scene.NAME_1
        self.fade_scene = FadeScene(self.back_NAME_1)
        self.temp_NAME_1 = pygame.NAME_1((800,600))
        self.swk_player = swk
        self.obstacle_group = pygame.sprite.Group()
        self.dx_dy = [0,0]
        self.win_pos_x = 0
        self.win_pos_y = 0
        self.init_actor()
        sound_path = os.path.join("./resource/sound/aigei.mp3")
        pygame.mixer.music.load(sound_path)
        pygame.mixer.music.play(-1)
    def init_actor(self):
        for group in self.tiled_scene.tiled.tmx_data.objectgroups:
            if isinstance(group, pytmx.TiledObjectGroup):
                if group.name == "god":
                    for obj in group:
                        self.earth_god = EarthGod(obj.x, obj.y)
                if group.name == "actor":
                    # if group.name =="play" :
                    for obj in group:
                        if obj.name == "sun":
                            # if obj.name == "swk":
                            self.swk_player.set_pos(obj.x, obj.y)
                            self.win_pos_x = obj.x - 400
                            # win_pos_x = obj.x - 100
                            self.win_pos_y = obj.y - 300
                            # win_pos_y = obj.y - 100
                            # 156 * 199   205 * 367
                if group.name == "obstacle":
                    for obj in group:
                        obs = pygame.sprite.Sprite()
                        obs.rect = pygame.Rect(obj.x, obj.y, obj.width, obj.height)
                        self.obstacle_group.add(obs)
                if group.name == "elder":
                   for obj in group:
                       if obj.name =="elder1":
                           self.elder = Elder(obj.x,obj.y)
                       if obj.name == "elder2