import curses


class ColorName:
    DEFAULT = -1
    BLACK = curses.COLOR_BLACK
    BLUE = curses.COLOR_BLUE
    CYAN = curses.COLOR_CYAN
    GREEN = curses.COLOR_GREEN
    MAGENTA = curses.COLOR_MAGENTA
    RED = curses.COLOR_RED
    WHITE = curses.COLOR_WHITE
    YELLOW = curses.COLOR_YELLOW

    COLOR0 = 0
    COLOR1 = 1
    COLOR2 = 2
    COLOR3 = 3
    COLOR4 = 4
    COLOR5 = 5
    COLOR6 = 6
    COLOR7 = 7
    COLOR8 = 8
    COLOR9 = 9
    COLOR10 = 10
    COLOR11 = 11
    COLOR12 = 12
    COLOR13 = 13
    COLOR14 = 14
    COLOR15 = 15
    COLOR16 = 16
    COLOR17 = 17
    COLOR18 = 18
    COLOR19 = 19
    COLOR20 = 20
    COLOR21 = 21
    COLOR22 = 22
    COLOR23 = 23
    COLOR24 = 24
    COLOR25 = 25
    COLOR26 = 26
    COLOR27 = 27
    COLOR28 = 28
    COLOR29 = 29
    COLOR30 = 30
    COLOR31 = 31
    COLOR32 = 32
    COLOR33 = 33
    COLOR34 = 34
    COLOR35 = 35
    COLOR36 = 36
    COLOR37 = 37
    COLOR38 = 38
    COLOR39 = 39
    COLOR40 = 40
    COLOR41 = 41
    COLOR42 = 42
    COLOR43 = 43
    COLOR44 = 44
    COLOR45 = 45
    COLOR46 = 46
    COLOR47 = 47
    COLOR48 = 48
    COLOR49 = 49
    COLOR50 = 50
    COLOR51 = 51
    COLOR52 = 52
    COLOR53 = 53
    COLOR54 = 54
    COLOR55 = 55
    COLOR56 = 56
    COLOR57 = 57
    COLOR58 = 58
    COLOR59 = 59
    COLOR60 = 60
    COLOR61 = 61
    COLOR62 = 62
    COLOR63 = 63
    COLOR64 = 64
    COLOR65 = 65
    COLOR66 = 66
    COLOR67 = 67
    COLOR68 = 68
    COLOR69 = 69
    COLOR70 = 70
    COLOR71 = 71
    COLOR72 = 72
    COLOR73 = 73
    COLOR74 = 74
    COLOR75 = 75
    COLOR76 = 76
    COLOR77 = 77
    COLOR78 = 78
    COLOR79 = 79
    COLOR80 = 80
    COLOR81 = 81
    COLOR82 = 82
    COLOR83 = 83
    COLOR84 = 84
    COLOR85 = 85
    COLOR86 = 86
    COLOR87 = 87
    COLOR88 = 88
    COLOR89 = 89
    COLOR90 = 90
    COLOR91 = 91
    COLOR92 = 92
    COLOR93 = 93
    COLOR94 = 94
    COLOR95 = 95
    COLOR96 = 96
    COLOR97 = 97
    COLOR98 = 98
    COLOR99 = 99
    COLOR100 = 100
    COLOR101 = 101
    COLOR102 = 102
    COLOR103 = 103
    COLOR104 = 104
    COLOR105 = 105
    COLOR106 = 106
    COLOR107 = 107
    COLOR108 = 108
    COLOR109 = 109
    COLOR110 = 110
    COLOR111 = 111
    COLOR112 = 112
    COLOR113 = 113
    COLOR114 = 114
    COLOR115 = 115
    COLOR116 = 116
    COLOR117 = 117
    COLOR118 = 118
    COLOR119 = 119
    COLOR120 = 120
    COLOR121 = 121
    COLOR122 = 122
    COLOR123 = 123
    COLOR124 = 124
    COLOR125 = 125
    COLOR126 = 126
    COLOR127 = 127
    COLOR128 = 128
    COLOR129 = 129
    COLOR130 = 130
    COLOR131 = 131
    COLOR132 = 132
    COLOR133 = 133
    COLOR134 = 134
    COLOR135 = 135
    COLOR136 = 136
    COLOR137 = 137
    COLOR138 = 138
    COLOR139 = 139
    COLOR140 = 140
    COLOR141 = 141
    COLOR142 = 142
    COLOR143 = 143
    COLOR144 = 144
    COLOR145 = 145
    COLOR146 = 146
    COLOR147 = 147
    COLOR148 = 148
    COLOR149 = 149
    COLOR150 = 150
    COLOR151 = 151
    COLOR152 = 152
    COLOR153 = 153
    COLOR154 = 154
    COLOR155 = 155
    COLOR156 = 156
    COLOR157 = 157
    COLOR158 = 158
    COLOR159 = 159
    COLOR160 = 160
    COLOR161 = 161
    COLOR162 = 162
    COLOR163 = 163
    COLOR164 = 164
    COLOR165 = 165
    COLOR166 = 166
    COLOR167 = 167
    COLOR168 = 168
    COLOR169 = 169
    COLOR170 = 170
    COLOR171 = 171
    COLOR172 = 172
    COLOR173 = 173
    COLOR174 = 174
    COLOR175 = 175
    COLOR176 = 176
    COLOR177 = 177
    COLOR178 = 178
    COLOR179 = 179
    COLOR180 = 180
    COLOR181 = 181
    COLOR182 = 182
    COLOR183 = 183
    COLOR184 = 184
    COLOR185 = 185
    COLOR186 = 186
    COLOR187 = 187
    COLOR188 = 188
    COLOR189 = 189
    COLOR190 = 190
    COLOR191 = 191
    COLOR192 = 192
    COLOR193 = 193
    COLOR194 = 194
    COLOR195 = 195
    COLOR196 = 196
    COLOR197 = 197
    COLOR198 = 198
    COLOR199 = 199
    COLOR200 = 200
    COLOR201 = 201
    COLOR202 = 202
    COLOR203 = 203
    COLOR204 = 204
    COLOR205 = 205
    COLOR206 = 206
    COLOR207 = 207
    COLOR208 = 208
    COLOR209 = 209
    COLOR210 = 210
    COLOR211 = 211
    COLOR212 = 212
    COLOR213 = 213
    COLOR214 = 214
    COLOR215 = 215
    COLOR216 = 216
    COLOR217 = 217
    COLOR218 = 218
    COLOR219 = 219
    COLOR220 = 220
    COLOR221 = 221
    COLOR222 = 222
    COLOR223 = 223
    COLOR224 = 224
    COLOR225 = 225
    COLOR226 = 226
    COLOR227 = 227
    COLOR228 = 228
    COLOR229 = 229
    COLOR230 = 230
    COLOR231 = 231
    COLOR232 = 232
    COLOR233 = 233
    COLOR234 = 234
    COLOR235 = 235
    COLOR236 = 236
    COLOR237 = 237
    COLOR238 = 238
    COLOR239 = 239
    COLOR240 = 240
    COLOR241 = 241
    COLOR242 = 242
    COLOR243 = 243
    COLOR244 = 244
    COLOR245 = 245
    COLOR246 = 246
    COLOR247 = 247
    COLOR248 = 248
    COLOR249 = 249
    COLOR250 = 250
    COLOR251 = 251
    COLOR252 = 252
    COLOR253 = 253
    COLOR254 = 254
    COLOR255 = 255

    # from http://ethanschoonover.com/solarized
    BASE03 = 234
    BASE02 = 235
    BASE01 = 240
    BASE00 = 241
    BASE0 = 244
    BASE1 = 245
    BASE2 = 254
    BASE3 = 230
    YELLOW = 136
    ORANGE = 166
    RED = 160
    MAGENTA = 125
    VIOLET = 61
    BLUE = 33
    CYAN = 37
    GREEN = 64

    # from tmux color.c
    _COLOR256_TO_16 = [
        0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
        0, 4, 4, 4, 12, 12, 2, 6, 4, 4, 12, 12, 2, 2, 6, 4,
        12, 12, 2, 2, 2, 6, 12, 12, 10, 10, 10, 10, 14, 12, 10, 10,
        10, 10, 10, 14, 1, 5, 4, 4, 12, 12, 3, 8, 4, 4, 12, 12,
        2, 2, 6, 4, 12, 12, 2, 2, 2, 6, 12, 12, 10, 10, 10, 10,
        14, 12, 10, 10, 10, 10, 10, 14, 1, 1, 5, 4, 12, 12, 1, 1,
        5, 4, 12, 12, 3, 3, 8, 4, 12, 12, 2, 2, 2, 6, 12, 12,
        10, 10, 10, 10, 14, 12, 10, 10, 10, 10, 10, 14, 1, 1, 1, 5,
        12, 12, 1, 1, 1, 5, 12, 12, 1, 1, 1, 5, 12, 12, 3, 3,
        3, 7, 12, 12, 10, 10, 10, 10, 14, 12, 10, 10, 10, 10, 10, 14,
        9, 9, 9, 9, 13, 12, 9, 9, 9, 9, 13, 12, 9, 9, 9, 9,
        13, 12, 9, 9, 9, 9, 13, 12, 11, 11, 11, 11, 7, 12, 10, 10,
        10, 10, 10, 14, 9, 9, 9, 9, 9, 13, 9, 9, 9, 9, 9, 13,
        9, 9, 9, 9, 9, 13, 9, 9, 9, 9, 9, 13, 9, 9, 9, 9,
        9, 13, 11, 11, 11, 11, 11, 15, 0, 0, 0, 0, 0, 0, 8, 8,
        8, 8, 8, 8, 7, 7, 7, 7, 7, 7, 15, 15, 15, 15, 15, 15
    ]

    def __init__(self):
        self.COLOR256_TO_16 = self._COLOR256_TO_16[:]
        try:
            self.num_colors = curses.tigetnum('colors')
        except curses.error:
            self.num_colors = 8

    def get(self, name):
        ret = getattr(self, name.upper())
        if ret < 0:   # name is 'default'
            return ret
        if self.num_colors < 256:
            ret = self.COLOR256_TO_16[ret]
        if self.num_colors < 16:
            ret = ret - 8 if ret >= 8 else ret
        return ret


class DarkPalette(ColorName):
    # from http://ethanschoonover.com/solarized
    BASE03 = 234
    BASE02 = 235
    BASE01 = 240
    BASE00 = 241
    BASE0 = 244
    BASE1 = 245
    BASE2 = 254
    BASE3 = 230

    LIGHTBLUE = ColorName.BLUE

    def __init__(self):
        super().__init__()

        self.COLOR256_TO_16[self.BASE02] = curses.COLOR_BLUE
        self.COLOR256_TO_16[self.BASE3] = curses.COLOR_WHITE
        self.COLOR256_TO_16[self.YELLOW] = curses.COLOR_YELLOW
        self.COLOR256_TO_16[self.WHITE] = curses.COLOR_WHITE

        if self.num_colors < 256:
            # todo:
            self.DEFAULT = -1
            self.BLACK = curses.COLOR_BLACK
            self.BLUE = curses.COLOR_BLUE
            self.CYAN = curses.COLOR_CYAN
            self.GREEN = curses.COLOR_GREEN
            self.MAGENTA = curses.COLOR_MAGENTA
            self.RED = curses.COLOR_RED
            self.WHITE = curses.COLOR_WHITE
            self.YELLOW = curses.COLOR_YELLOW

            self.ORANGE = curses.COLOR_MAGENTA
            self.LIGHTBLUE = curses.COLOR_CYAN


class LightPalette(ColorName):
    # from http://ethanschoonover.com/solarized
    BASE3 = 234
    BASE2 = 235
    BASE1 = 240
    BASE0 = 241
    BASE00 = 244
    BASE01 = 245
    BASE02 = 254
    BASE03 = 230

    LIGHTBLUE = ColorName.CYAN

    def __init__(self):
        super().__init__()

        self.COLOR256_TO_16[self.BASE02] = curses.COLOR_BLUE
        self.COLOR256_TO_16[self.BASE3] = curses.COLOR_CYAN
        self.COLOR256_TO_16[self.YELLOW] = curses.COLOR_YELLOW
        self.COLOR256_TO_16[self.WHITE] = curses.COLOR_WHITE

        if self.num_colors < 256:
            # todo:
            self.DEFAULT = -1
            self.BLACK = curses.COLOR_BLACK
            self.BLUE = curses.COLOR_BLUE
            self.CYAN = curses.COLOR_CYAN
            self.GREEN = curses.COLOR_GREEN
            self.MAGENTA = curses.COLOR_MAGENTA
            self.RED = curses.COLOR_RED
            self.WHITE = curses.COLOR_WHITE
            self.YELLOW = curses.COLOR_YELLOW

            self.ORANGE = curses.COLOR_MAGENTA
            self.LIGHTBLUE = curses.COLOR_CYAN


class Colors:

    def __init__(self, palette):
        self.colornames = palette

        self.pairs = {
            (ColorName.WHITE, ColorName.BLACK): 0
        }

    def get_color(self, fg, bg):
        if (fg, bg) not in self.pairs:
            n = len(self.pairs)
            curses.init_pair(n, fg, bg)
            self.pairs[(fg, bg)] = curses.color_pair(n)

        return self.pairs[(fg, bg)]
