/****************************************************************************
 Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
 Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md).

 https://axmol.dev/

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 ****************************************************************************/

#include "MainScene.h"

using namespace ax;

static int s_sceneID = 1000;

// Print useful error message instead of segfaulting when files are not there.
static void problemLoading(const char* filename)
{
    printf("Error while loading: %s\n", filename);
    printf(
        "Depending on how you compiled you might have to add 'Content/' in front of filenames in "
        "MainScene.cpp\n");
}

// on "init" you need to initialize your instance
bool MainScene::init()
{
    //////////////////////////////
    // 1. super init first
    if (!Scene::init())
    {
        return false;
    }

    auto visibleSize = _director->getVisibleSize();
    auto origin      = _director->getVisibleOrigin();
    auto safeArea    = _director->getSafeAreaRect();

    // 2. Add a menu item with "X" image, which is clicked to quit the program
    auto closeItem = MenuItemImage::create("CloseNormal.png", "CloseSelected.png",
                                           AX_CALLBACK_1(MainScene::menuCloseCallback, this));

    if (closeItem != nullptr)
    {
        float x = origin.x + visibleSize.width - closeItem->getContentSize().width / 2;
        float y = origin.y + closeItem->getContentSize().height / 2;
        closeItem->setPosition(Vec2(x, y));
    }

    auto menu = Menu::create(closeItem, NULL);
    menu->setPosition(Vec2::ZERO);
    this->addChild(menu, 1);

    /////////////////////////////
    // 3. 3D Setup
    
    // Grid settings
    const int gridSize = 10;
    const float cubeDim = 1.0f;
    const float spacing = 0.25f;
    const float step = cubeDim + spacing;
    
    // Calculate center of the grid
    // Grid goes from 0 to (gridSize-1)*step
    float gridWidth = (gridSize - 1) * step;
    Vec3 gridCenter(gridWidth / 2.0f, 0.0f, gridWidth / 2.0f);

    // Create a single cube at the center of the grid area
    auto centerCube = MeshRenderer::create("cube.obj");
    if (centerCube)
    {
        centerCube->setPosition3D(gridCenter);
        centerCube->setCameraMask((unsigned short)CameraFlag::USER1);
        this->addChild(centerCube);
    }

    // Add Lights
    // Directional Light: Like sunlight, creating shadows/depth
    auto dirLight = DirectionLight::create(Vec3(-1.0f, -1.0f, -0.5f), Color3B::WHITE);
    dirLight->setCameraMask((unsigned short)CameraFlag::USER1);
    this->addChild(dirLight);

    // Ambient Light: To ensure dark sides are still somewhat visible
    auto ambLight = AmbientLight::create(Color3B(80, 80, 80));
    ambLight->setCameraMask((unsigned short)CameraFlag::USER1);
    this->addChild(ambLight);

    // Add a Ground Plane
    auto ground = MeshRenderer::create("cube.obj");
    if (ground)
    {
        // Calculate the exact size needed to cover the grid
        // Grid size is (gridSize-1)*step + cubeDim
        float actualSize = gridWidth + cubeDim;
        float padding = 2.0f; // Increased padding to 2.0
        
        ground->setScaleX(actualSize + padding);
        ground->setScaleZ(actualSize + padding);
        ground->setScaleY(0.25f); // Thickness set to 0.25
        // Positioned so the top surface is at Y = -0.5 (cubes are at Y=0 with height 1)
        ground->setPosition3D(Vec3(gridCenter.x, -0.5f - 0.125f, gridCenter.z));
        ground->setCameraMask((unsigned short)CameraFlag::USER1);
        ground->setColor(Color3B(100, 100, 100)); 
        this->addChild(ground);
    }

    // Setup Camera for Quarter View (Isometric-like)
    _camera3D = Camera::createPerspective(60.0f, visibleSize.width / visibleSize.height, 0.1f, 1000.0f);
    _camera3D->setCameraFlag(CameraFlag::USER1);
    
    // Initial camera position setup
    _targetPos = gridCenter;
    _pitch = 45.0f;
    _yaw = 45.0f;
    _distance = 25.0f;
    updateCameraPosition();
    
    this->addChild(_camera3D);

    // Mouse Listener
    _mouseListener                = EventListenerMouse::create();
    _mouseListener->onMouseMove   = AX_CALLBACK_1(MainScene::onMouseMove, this);
    _mouseListener->onMouseUp     = AX_CALLBACK_1(MainScene::onMouseUp, this);
    _mouseListener->onMouseDown   = AX_CALLBACK_1(MainScene::onMouseDown, this);
    _mouseListener->onMouseScroll = AX_CALLBACK_1(MainScene::onMouseScroll, this);
    _eventDispatcher->addEventListenerWithSceneGraphPriority(_mouseListener, this);

    // Disable the default 2D camera for these 3D objects by using CameraFlag::USER1
    // The label and menu will use the default camera (CameraFlag::DEFAULT)

    // Add a label for confirmation
    auto label = Label::createWithTTF("Center Cube", "fonts/Marker Felt.ttf", 24);
    if (label)
    {
        label->setPosition(Vec2(origin.x + visibleSize.width / 2, origin.y + visibleSize.height - 30));
        this->addChild(label, 1);
    }

    scheduleUpdate();

    return true;
}

void MainScene::onTouchesBegan(const std::vector<ax::Touch*>& touches, ax::Event* event)
{
    for (auto&& t : touches)
    {
        // AXLOGD("onTouchesBegan detected, X:{}  Y:{}", t->getLocation().x, t->getLocation().y);
    }
}

void MainScene::onTouchesMoved(const std::vector<ax::Touch*>& touches, ax::Event* event)
{
    for (auto&& t : touches)
    {
        // AXLOGD("onTouchesMoved detected, X:{}  Y:{}", t->getLocation().x, t->getLocation().y);
    }
}

void MainScene::onTouchesEnded(const std::vector<ax::Touch*>& touches, ax::Event* event)
{
    for (auto&& t : touches)
    {
        // AXLOGD("onTouchesEnded detected, X:{}  Y:{}", t->getLocation().x, t->getLocation().y);
    }
}

bool MainScene::onMouseDown(Event* event)
{
    EventMouse* e = static_cast<EventMouse*>(event);
    if (e->getMouseButton() == EventMouse::MouseButton::BUTTON_LEFT)
    {
        _isMouseDown = true;
        _lastMousePos = e->getLocationInView();
    }
    return true;
}

bool MainScene::onMouseUp(Event* event)
{
    EventMouse* e = static_cast<EventMouse*>(event);
    if (e->getMouseButton() == EventMouse::MouseButton::BUTTON_LEFT)
    {
        _isMouseDown = false;
    }
    return true;
}

bool MainScene::onMouseMove(Event* event)
{
    EventMouse* e = static_cast<EventMouse*>(event);
    Vec2 currentPos = e->getLocationInView();
    
    if (_isMouseDown)
    {
        Vec2 delta = currentPos - _lastMousePos;
        float sensitivity = 0.15f; // 감도 조절 (기존 0.5f에서 하향)
        
        _yaw += delta.x * sensitivity;
        _pitch += delta.y * sensitivity; // 마우스 이동 방향에 맞춰 상하 각도 변경

        // Clamp pitch to avoid flipping (수직 방향 각도 제한)
        _pitch = std::clamp(_pitch, 10.0f, 85.0f);

        updateCameraPosition();
    }
    _lastMousePos = currentPos;
    return true;
}

bool MainScene::onMouseScroll(Event* event)
{
    EventMouse* e = static_cast<EventMouse*>(event);
    float zoomSensitivity = 0.5f; // 줌 감도 조절
    _distance -= e->getScrollY() * zoomSensitivity;
    
    // Clamp distance
    _distance = std::clamp(_distance, 5.0f, 100.0f);
    
    updateCameraPosition();
    return true;
}

void MainScene::updateCameraPosition()
{
    if (!_camera3D) return;

    // Convert spherical coordinates to Cartesian
    float pitchRad = AX_DEGREES_TO_RADIANS(_pitch);
    float yawRad = AX_DEGREES_TO_RADIANS(_yaw);

    float x = _distance * cosf(pitchRad) * cosf(yawRad);
    float y = _distance * sinf(pitchRad);
    float z = _distance * cosf(pitchRad) * sinf(yawRad);

    _camera3D->setPosition3D(_targetPos + Vec3(x, y, z));
    _camera3D->lookAt(_targetPos, Vec3(0, 1, 0));
}

void MainScene::onKeyPressed(EventKeyboard::KeyCode code, Event* event)
{
    AXLOGD("Scene: #{} onKeyPressed, keycode: {}", _sceneID, static_cast<int>(code));
}

void MainScene::onKeyReleased(EventKeyboard::KeyCode code, Event* event)
{
    AXLOGD("onKeyReleased, keycode: {}", static_cast<int>(code));
}

void MainScene::update(float delta)
{
    switch (_gameState)
    {
    case GameState::init:
    {
        _gameState = GameState::update;
        break;
    }

    case GameState::update:
    {
        /////////////////////////////
        // Add your codes below...like....
        //
        // UpdateJoyStick();
        // UpdatePlayer();
        // UpdatePhysics();
        // ...
        break;
    }

    case GameState::pause:
    {
        /////////////////////////////
        // Add your codes below...like....
        //
        // anyPauseStuff()

        break;
    }

    case GameState::menu1:
    {  /////////////////////////////
        // Add your codes below...like....
        //
        // UpdateMenu1();
        break;
    }

    case GameState::menu2:
    {  /////////////////////////////
        // Add your codes below...like....
        //
        // UpdateMenu2();
        break;
    }

    case GameState::end:
    {  /////////////////////////////
        // Add your codes below...like....
        //
        // CleanUpMyCrap();
        menuCloseCallback(this);
        break;
    }

    }  // switch
}

void MainScene::menuCloseCallback(ax::Object* sender)
{
    // Close the axmol game scene and quit the application
    _director->end();

    /*To navigate back to native iOS screen(if present) without quitting the application  ,do not use
     * _director->end() as given above,instead trigger a custom event created in RootViewController.mm
     * as below*/

    // EventCustom customEndEvent("game_scene_close_event");
    //_eventDispatcher->dispatchEvent(&customEndEvent);
}

MainScene::MainScene()
{
    _sceneID = ++s_sceneID;
    AXLOGD("Scene: ctor: #{}", _sceneID);
}

MainScene::~MainScene()
{
    AXLOGD("~Scene: dtor: #{}", _sceneID);

    if (_touchListener)
        _eventDispatcher->removeEventListener(_touchListener);
    if (_keyboardListener)
        _eventDispatcher->removeEventListener(_keyboardListener);
    if (_mouseListener)
        _eventDispatcher->removeEventListener(_mouseListener);
    _sceneID = -1;
}
