# encoding: UTF-8
#
# Copyright (c) 2010-2017 GoodData Corporation. All rights reserved.
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

require_relative '../rest/resource'

module GoodData
  class StyleSetting < Rest::Resource
    STYLE_SETTING_PATH = '/gdc/projects/%s/styleSettings'

    EMPTY_OBJECT = {
      'styleSettings' => {
        'chartPalette' => []
      }
    }

    attr_reader :colors

    class << self
      def current(opts = { client: GoodData.connection, project: GoodData.project })
        client, project = GoodData.get_client_and_project(opts)
        uri = STYLE_SETTING_PATH % project.pid
        data = client.get(uri)
        client.create(StyleSetting, data)
      end

      def create(colors, opts = { client: GoodData.connection, project: GoodData.project })
        client, project = GoodData.get_client_and_project(opts)
        if colors.is_a?(StyleSetting)
          colors = colors.colors
        else
          colors = colors.uniq
        end
        uri = STYLE_SETTING_PATH % project.pid
        data_to_send = GoodData::Helpers.deep_dup(EMPTY_OBJECT).tap do |d|
          d['styleSettings']['chartPalette'] = colors.map { |color| GoodData::Helpers.stringify_keys(color) }
        end
        style = client.create(StyleSetting, data_to_send)
        client.put(uri, data_to_send)
        style
      end

      def reset(opts = { client: GoodData.connection, project: GoodData.project })
        client, project = GoodData.get_client_and_project(opts)
        uri = STYLE_SETTING_PATH % project.pid
        client.delete(uri)
      end
    end

    def initialize(json)
      super
      @json = json
      @colors = json ? data['chartPalette'] : []
    end

    def empty?
      colors.empty?
    end
  end
end
