# -*- coding: latin-1 -*-
class Playfair:

    def __init__(self, klartext, keytext):
        self.klartext = klartext
        self.keytext = keytext
        codiertext = ''
        codieralphabet = ''
        self.globall = ''

    def textformatieren(self):
        a = str(self.klartext)
        a = self.klartext.upper()
        text = ''
        for i in range(len(a)):
            if a[i] == 'Ä':
                text = text + 'AE'
            elif a[i] == 'Ö':
                text = text + 'OE'
            elif a[i] == 'Ü':
                text = text + 'UE'
            elif a[i] == 'ß':
                text = text + 'SZ'
            elif a[i] == '"':
                text = text + ''
            elif a[i] == "'":
                text = text + ''
            elif a[i] == ".":
                text = text + ''
            elif a[i] == " ":
                text = text + ''
            elif a[i] == "J":
                text = text + 'I'
            else:
                text = text +  a[i]
        text2=''
        for e in range(len(text)-1):
            if text[e] == text[e+1]:
                text2 = text2 + text[e] + "X" #bei doppelten Zeichen nur erstes beachten
            else:
                text2 = text2 + text[e]
        text2 = text2 + text[len(text)-1]
        if len(text2) % 2 == 1:
            text2 =text2 + "X"
        self.codiertext = text2
        print text2

    def alphabetisieren(self):
        b = str(self.keytext)
        b = self.keytext.upper()
        text = ''
        keytextalphabet = 'ABCDEFGHIKLMNOPQRSTUVWXYZ'
        #erweitert mit der Begruendung s.u.
        b += keytextalphabet
        for i in range(len(b)):
            if b[i] == 'Ä':
                text = text + 'AE'
            elif b[i] == 'Ö':
                text = text + 'OE'
            elif b[i] == 'Ü':
                text = text + 'UE'
            elif b[i] == 'ß':
                text = text + 'SZ'
            elif b[i] == '"':
                text = text + ''
            elif b[i] == "'":
                text = text + ''
            elif b[i] == ".":
                text = text + ''
            elif b[i] == " ":
                text = text + ''
            elif b[i] == "J":
                text = text + 'I'
            else:
                text = text +  b[i]
        text3 = ''
        keytextneu = ''
        for e in range(len(text)):
            if text3.find(text[e]) == -1:
                text3 = text3 + text[e]
        self.codieralphabet = text3
        print text3

    def codieren(self):
        #codiertext = self.textformatieren.text2
        ausgabetext=''
        final=''
        while len(self.codiertext) <> 0:
            erstebeide = self.codiertext[0:2]
            pos1 = self.codieralphabet.find(erstebeide[0])
            pos2 = self.codieralphabet.find(erstebeide[1])
            self.codiertext = self.codiertext[2:]
            y1 = pos1 / 5
            x1 = pos1 % 5
            y2 = pos2 / 5
            x2 = pos2 % 5
            if y1 == y2:
                if x1 == 4:
                    x1 = 0
                else: x1 += 1
                if x2 == 4:
                    x2 = 0
                else: x2 += 1
                z1 = y1 * 5 + x1
                z2 = y2 * 5 + x2
            else:
                if x1 == x2:
                    if y1 == 4:
                        y1 = 0
                    else: y1 += 1
                    if y2 == 4:
                        y2 = 0
                    else: y2 += 1
                    z1 = y1 * 5 + x1
                    z2 = y2 * 5 + x2
                else:
                    z1 = 5 * y1 + x2
                    z2 = 5 * y2 + x1
            ausgabetext = ausgabetext + self.codieralphabet[z1]+self.codieralphabet[z2]
            self.globall = ausgabetext
        return "global:" + self.globall

