class 'Game1' (Script)

function Game1:__init() super()
    
    self.load=false
--    self.rotationCenters = {    Position(0, 27), Position(50, 54),
--                                Position(51, 4), Position(12, 75),
--                                Position(43, 27), Position(12, 54)}
--
--
--    self.correctAngles = {  {min=205, max=225}, {min=174, max=187}, -- ~ 10 25
--                            {min=271, max=295}, {min=100, max=140}, -- ~ 6 20
--                            {min=70, max=100}, {min=190, max=210}}  -- ~ 1 40
--
    self.correctState = {   false,false,
                            false,false,
                           false,false}
--
--    self.rotationObjectIndex = nil;
--    self.light={}
  self.numberComplite = {}
end

function Game1:on_attach()
    self.rotationCenters = {    Position(0, 27), Position(50, 54),
        Position(51, 4), Position(12, 75),
        Position(43, 27), Position(12, 54)}


    self.correctAngles = {  {min=205, max=225}, {min=174, max=187}, -- ~ 10 25
        {min=271, max=295}, {min=100, max=140}, -- ~ 6 20
        {min=70, max=100}, {min=190, max=210}}  -- ~ 1 40




    self.rotationObjectIndex = nil;
    self.light={}
   



    local scene = self:scene()
    
    self.hourHands = {  scene:object('smallhourhand1'), scene:object('bighourhand1'),
                        scene:object('smallhourhand2'), scene:object('bighourhand2'),
                        scene:object('smallhourhand3'), scene:object('bighourhand3') }
--    self.light={scene:object('light_bluebig'), scene:object('light_greenbig'), scene:object('light_redbig') }
--    for i=1,#self.light do
--        self.light[i]:hide()
--    end
    self.load=false
end

function Game1:on_lb_down()

    local scene = self:scene()
    local selection = scene:overed_object()
    
    if selection then
        for i=1,#self.hourHands do
            local obj = self.hourHands[i]
            if selection:name() == obj:name() and self:flagMove(i) then
                self:rotate(selection, i)
                break
            end
        end
    end
end

function Game1:rotate(obj, index)
    self.rotationObjectIndex = index    
    self:begin_rotate(obj, self.rotationCenters[index])
end

function Game1:on_lb_up()
    if self.rotationObjectIndex ~= nil then
        local obj = self.hourHands[self.rotationObjectIndex]
        self:end_rotate(obj)
        
        self:checkAngle()
      
        self.rotationObjectIndex = nil
    end
end

function Game1:checkAngle()
    local obj   = self.hourHands[self.rotationObjectIndex]
    local angle = obj:angle()
    --print(self.correctAngles[self.rotationObjectIndex].min.."+"..self.correctAngles[self.rotationObjectIndex].max.."+"..angle..'+'..self.rotationObjectIndex)
    if angle >= self.correctAngles[self.rotationObjectIndex].min and angle <= self.correctAngles[self.rotationObjectIndex].max then
        self.correctState[self.rotationObjectIndex] = true;
        self:complite_oneFlowers()

         --table.insert(self.numberComplite,self.rotationObjectIndex)

        self:checkComplete()

    end
end

function Game1:complite_oneFlowers()
    if  self.correctState[1] and self.correctState[2] then
      table.insert(self.numberComplite,1)
      table.insert(self.numberComplite,2)
      self.hourHands[1]:set_angle_saveable()
      self.hourHands[2]:set_angle_saveable()
      self:scene():execute_action('flower_blue')
    end
    if  self.correctState[3] and self.correctState[4] then
        table.insert(self.numberComplite,3)
        table.insert(self.numberComplite,4)
        self.hourHands[3]:set_angle_saveable()
        self.hourHands[4]:set_angle_saveable()
        self:scene():execute_action('flower_green')
    end
    if  self.correctState[5] and self.correctState[6] then
       table.insert(self.numberComplite,5)
       table.insert(self.numberComplite,6)

       self.hourHands[5]:set_angle_saveable()
       self.hourHands[6]:set_angle_saveable()
        self:scene():execute_action('flower_yelow')
    end
end

function Game1:flagMove(m)
    for i=1,#self.numberComplite do
       if self.numberComplite[i]==m then
          return false
       end
    end
    return true
end



function Game1:checkComplete()
    local isComplete = true

    for i=1,#self.correctState do
        if not self.correctState[i] then
            isComplete = false
            break
        end
    end
    
    if isComplete then
       self:scene():execute_action('complete_game1')
    end
end

function Game1:save(os)
    os:save(0) -- version
    os:save(true)
    for i=1,6 do
        os:save(self.correctState[i])
    end

    os:save(#self.numberComplite)
     for i=1,#self.numberComplite do
         os:save(self.numberComplite[i])
     end
    end

function Game1:load(is)
    local ver = is:load()
    self.load = is:load()
    for i=1,6 do
     self.correctState[i]=is:load()
    end
    for i=1,is:load() do
        self.numberComplite[i]= is:load()
    end
end
