class controller:
    LOGGING_FORMAT = '%(asctime)s %(levelname)s: %(message)s'
    DATE_FORMAT = '%Y%m%d %H:%M:%S'
    DATE_TODAY = '{}{}{}'.format(datetime.datetime.now().year,datetime.datetime.now().month,datetime.datetime.now().day)
    LOG_FILE = './logs/Logs_{}.log'.format(DATE_TODAY)
    if not os.path.exists(LOG_FILE):
        files = open(LOG_FILE,"w+")
        files.close()
    logging.basicConfig(level=logging.INFO, filename=LOG_FILE, format=LOGGING_FORMAT, datefmt=DATE_FORMAT)
    def __init__(self):
        #self.ADB = ADB(device_Name='127.0.0.1:62001',screen_Size=(720,1280))  #夜神
        self.ADB = ADB(device_Name='emulator-5556',screen_Size=(560,960))

    def Game_Start(self):
        logging.info('啟動遊戲({}),等待 5 秒'.format('air.jp.co.cygames.worldflipper/.AppEntry'))
        self.ADB.Start_Game(Game_Activity_Name='air.jp.co.cygames.worldflipper/.AppEntry')
        time.sleep(5)

    def Game_Stop(self):
        logging.info('關閉遊戲({}),等待 2 秒'.format('air.jp.co.cygames.worldflipper'))
        self.ADB.Shut_Down_Game(Game_Activity_Name='air.jp.co.cygames.worldflipper')
        time.sleep(2)

    def Delete_Record(self):
        logging.info('為了刪除紀錄,開始判斷是否進入遊戲主頁面')
        print('為了刪除紀錄,開始判斷是否進入遊戲主頁面(透過右上角button)')
        self.Check_Stat(mode='delete-button')
        logging.info('已進入遊戲主頁面,開始刪除紀錄')
        print('開始刪除紀錄')
        self.ADB.Touch(500,56)  ##右上角選單
        time.sleep(2)
        self.ADB.Touch(392,482)  ##清除紀錄選項(垃圾桶)
        time.sleep(2)
        self.ADB.Touch(395,635)  ## OK (red-button)
        time.sleep(1)
        self.ADB.Touch(395,635)  ## OK_double-check (red-button)
        time.sleep(8)
        ##通信中,等待遊戲重啟
        #function 判斷是否到遊戲啟動首頁
        ## goto login_game

    def Login_Game(self,achiveTime='firstTime'):
        logging.info('開始判斷是否進入遊戲主頁面')
        print('開始判斷是否進入遊戲主頁面(透過右上角button)')
        self.Check_Stat(mode='delete-button')
        logging.info('已進入遊戲主頁面,點擊螢幕進入遊戲')
        print('已進入遊戲主頁面,點擊螢幕進入遊戲')
        self.ADB.Touch(395,635) ##點擊螢幕
        if achiveTime == 'firstTime':
            logging.info('新帳號第一次進入遊戲,開始判斷是否出現同意')
            print('新帳號第一次進入遊戲,開始判斷是否出現同意')
            self.Check_Stat(mode='agree-button')
            logging.info('已出現,點擊同意')
            print('已出現,點擊同意')
            self.ADB.Touch(395,635)  ##同意
            logging.info('開始判斷是否是否出現新手教學')
            print('開始判斷是否是否出現新手教學')
            self.Check_Stat(mode='canceltutorial-button')
            logging.info('已出現,點擊取消')
            print('已出現,點擊取消')
            self.ADB.Touch(151,623)  ##取消 (red-button)
            time.sleep(2)
            self.ADB.Touch(151,623)  ##取消 again(red-button)
            time.sleep(2)
            self.ADB.Touch(273,803)  ##ok again(red-button)
            time.sleep(2)
            for i in range(5):
                self.ADB.Touch(385,901)  ##下一步x5
                time.sleep(0.8)
            logging.info('開始判斷是否是否出現取名ok')
            print('開始判斷是否是否出現取名ok')
            self.Check_Stat(mode='nameconfirm-button')  ##判斷是否出現取名ok
            logging.info('已出現,確認取名')
            print('已出現,確認取名')
            self.ADB.Touch(269,465) ##ok (green-button)
            time.sleep(1.5)
            self.ADB.Touch(395,635)  ## OK (red-button)
        ##通信中
        ##function 判斷是否出現抽卡區
        time.sleep(2)
        ## goto summoning
    
    def Summoning(self):
        logging.info('開始判斷是否是否找到抽卡區')
        print('開始判斷是否是否找到抽卡區')  #這邊太慢
        self.ADB.Image_Grab(mode='get_summon-button')
        stat = self.ADB.Recognize_Img(mode='summon-button')
        while stat != True:
            self.ADB.Image_Grab(mode='get_summon-button')
            stat = self.ADB.Recognize_Img(mode='summon-button')
        logging.info('已看到抽卡區,點擊進入抽卡區')
        print('已看到抽卡區,點擊進入抽卡區')
        self.ADB.Touch(407,933)  ##進入抽卡區
        time.sleep(1.5)
        self.ADB.Touch(233,663)  ##進行強制單抽  (保底四星,有機會出五星)
        time.sleep(1.5)
        self.ADB.Touch(395,635)  ## OK (red-button)
        logging.info('開始判斷是否找到skip按鈕')
        print('開始判斷是否找到skip按鈕')
        self.Check_Stat(mode='skip-button')
        logging.info('已找到,按下skip')
        print('已找到,按下skip')
        self.ADB.Touch(529,25)  #skip
        logging.info('開始判斷是否是否抽完了')
        print('開始判斷是否是否抽完了')
        self.ADB.Image_Grab(mode='get_got4starconfirm-button')
        stat = self.ADB.Recognize_Img(mode='got4starconfirm-button')
        while stat != True:
            self.ADB.Touch(529,25)
            self.ADB.Image_Grab(mode='get_got4starconfirm-button')
            stat = self.ADB.Recognize_Img(mode='got4starconfirm-button')
        logging.info('已抽完,按下OK確認')
        print('已抽完,按下OK確認')
        time.sleep(1)
        self.ADB.Touch(272,620)  ##OK  
        time.sleep(1)
        self.ADB.Touch(476,833)  ##回上一頁
        ##讀取中
        logging.info('開始判斷是否是否出現送禮')
        print('開始判斷是否是否出現送禮')
        self.ADB.Image_Grab(mode='get_gotpriceconfirm-button')
        stat = self.ADB.Recognize_Img(mode='gotpriceconfirm-button')
        while stat != True:  #判斷是否出現送禮
            self.ADB.Touch(529,25)  #連點
            self.ADB.Image_Grab(mode='get_gotpriceconfirm-button')
            stat = self.ADB.Recognize_Img(mode='gotpriceconfirm-button')
        logging.info('已收到送禮,按下接收')
        print('已收到送禮,按下接收')
        self.ADB.Touch(351,641)  ##接收禮物按鈕
        logging.info('開始判斷是否是否出現ok')
        print('開始判斷是否是否出現ok')
        self.ADB.Image_Grab(mode='get_afterpriceconfirmok-button')
        stat = self.ADB.Recognize_Img(mode='afterpriceconfirmok-button')
        while stat != True:  ##判斷是否出現ok
            self.ADB.Touch(529,25)  #連點
            self.ADB.Image_Grab(mode='get_afterpriceconfirmok-button')
            stat = self.ADB.Recognize_Img(mode='afterpriceconfirmok-button')
        logging.info('已出現,按下OK確認')
        print('已出現,按下OK確認')
        self.ADB.Touch(351,641)  ##OK
        time.sleep(1)
        logging.info('進行十連抽')
        self.ADB.Touch(351,641)  ##十連抽
        time.sleep(1)
        self.ADB.Touch(351,641)  ##十連抽-確定
        logging.info('開始判斷是否找到skip按鈕')
        print('開始判斷是否找到skip按鈕')
        self.Check_Stat(mode='skip-button')
        logging.info('已找到,按下skip')
        print('已找到,按下skip')
        self.ADB.Touch(529,25)  #skip
        time.sleep(1)
        ## force-stop Game
    
    def Check_Box(self):
        logging.info('判斷是否出現包包選項')
        print('開始判斷是否出現包包選項')
        self.ADB.Image_Grab(mode='get_member-button')
        stat = self.ADB.Recognize_Img(mode='member-button')
        while stat != True:  ##function 判斷是否出現包包
            self.ADB.Touch(529,25)
            self.ADB.Touch(229,928)
            self.ADB.Image_Grab(mode='get_member-button')
            stat = self.ADB.Recognize_Img(mode='member-button')
        logging.info('已出現,打開包包')
        print('已出現,打開包包')
        time.sleep(1)
        self.ADB.Touch(495,105)  ##打開包包
        time.sleep(4)
        ## goto star Analysis

    def Star_Analysis(self):
        logging.info('擷取包包內照片')
        print('開始擷取包包內照片')
        self.ADB.Image_Grab(mode='get_star')
        logging.info('判斷角色星數')
        print('開始判斷角色星數')
        starNumber = self.ADB.Recognize_Img(mode='star')
        self.ADB.Touch(35,921)  ##離開角色包包
        if starNumber >=1:
            logging.info('得到結果,獲得五星角色 {} 隻,開始截圖保存至指定目錄'.format(starNumber))
            print('得到結果,獲得五星角色 {} 隻,開始截圖保存至指定目錄'.format(starNumber))
            self.ADB.Game_ScreenHot_By_Adb(save_path='./image/account/waitForAnalysis.jpg')
        return starNumber

    def Get_Account_ID(self):
        logging.info('判斷是否已回到首頁')
        print('開始判斷是否已回到首頁')
        self.ADB.Image_Grab(mode='get_summon-button')
        stat = self.ADB.Recognize_Img(mode='summon-button')
        while stat != True:
            self.ADB.Touch(35,921)  ##上一頁
            self.ADB.Image_Grab(mode='get_summon-button')
            stat = self.ADB.Recognize_Img(mode='summon-button')
        logging.info('已回到首頁,進入\'其他\'選單')
        print('已回到首頁,進入\'其他\'選單')
        self.ADB.Touch(492,921)  #進入'其他'選單
        time.sleep(1)
        self.ADB.Touch(267,851)  #點擊引繼
        logging.info('判斷是否已進到準備引繼的選單')
        print('開始判斷是否已進到準備引繼的選單')
        self.ADB.Image_Grab(mode='get_setaccount-button')
        stat = self.ADB.Recognize_Img(mode='setaccount-button')
        while stat != True:
            self.ADB.Image_Grab(mode='get_setaccount-button')
            stat = self.ADB.Recognize_Img(mode='setaccount-button')
        logging.info('已進入,開始引繼')
        print('已進入,開始引繼')
        time.sleep(0.5)
        self.ADB.Touch(265,583)  #點擊開始引繼
        time.sleep(1)
        self.ADB.Touch(388,816)  #ok
        time.sleep(1)
        self.ADB.Touch(266,490)  #點擊使用引繼碼
        time.sleep(1)
        self.ADB.Touch(270,659)  #ok
        time.sleep(1)
        self.ADB.Touch(263,566)  #點擊生成引繼碼
        time.sleep(1)
        self.ADB.Touch(261,354)  #點擊密碼框
        time.sleep(0.5)
        self.ADB.Text_Input('4700worldFlipper')
        time.sleep(0.5)
        self.ADB.Touch(261,413)  #點擊確認密碼框
        time.sleep(0.5)
        self.ADB.Text_Input('4700worldFlipper')
        time.sleep(0.5)
        self.ADB.Touch(70,597)  #勾選同意條款
        time.sleep(0.5)
        self.ADB.Touch(268,700)  #下一步
        time.sleep(1)
        #通信中

    def Recognize_Account(self,starNumber=0):
        logging.info('判斷是否已引繼成功得到引繼碼')
        print('開始判斷是否已引繼成功得到引繼碼')
        self.ADB.Image_Grab(mode='get_accountconfirm-button')
        stat = self.ADB.Recognize_Img(mode='accountconfirm-button')
        while stat != True:
            self.ADB.Image_Grab(mode='get_accountconfirm-button')
            stat = self.ADB.Recognize_Img(mode='accountconfirm-button')
        account = self.ADB.Image_Grab(mode='get_account')
        logging.info('成功獲得。將獲得的引繼碼({})存入指定目錄'.format(account))
        print('開始將獲得的引繼碼({})存入指定目錄'.format(account))
        os.rename('./image/account/waitForAnalysis.jpg','./image/account/{}_fiveStar/{}.jpg'.format(starNumber,account))

    def Check_Stat(self,mode=None):
        time.sleep(3)
        self.ADB.Image_Grab(mode=('get_'+mode))
        stat = self.ADB.Recognize_Img(mode=mode)
        while stat != True:
            #print('目前狀態: ', stat)
            self.ADB.Image_Grab(mode=('get_'+mode))
            stat = self.ADB.Recognize_Img(mode=mode)
class controller:
    LOGGING_FORMAT = '%(asctime)s %(levelname)s: %(message)s'
    DATE_FORMAT = '%Y%m%d %H:%M:%S'
    DATE_TODAY = '{}{}{}'.format(datetime.datetime.now().year,
                                 datetime.datetime.now().month,
                                 datetime.datetime.now().day)
    LOG_FILE = './logs/Logs_{}.log'.format(DATE_TODAY)
    if not os.path.exists(LOG_FILE):
        files = open(LOG_FILE, "w+")
        files.close()
    logging.basicConfig(level=logging.INFO,
                        filename=LOG_FILE,
                        format=LOGGING_FORMAT,
                        datefmt=DATE_FORMAT)

    def __init__(self):
        #self.ADB = ADB(device_Name='127.0.0.1:62001',screen_Size=(720,1280))  #夜神
        self.ADB = ADB(device_Name='emulator-5554', screen_Size=(560, 960))
        #self.ADB = ADB(device_Name='emulator-5556',screen_Size=(560,960))

    def Game_Start(self):
        logging.info('啟動遊戲({}),等待 5 秒'.format(
            'air.jp.co.cygames.worldflipper/.AppEntry'))
        self.ADB.Start_Game(
            Game_Activity_Name='air.jp.co.cygames.worldflipper/.AppEntry')
        time.sleep(5)

    def Game_Stop(self):
        logging.info(
            '關閉遊戲({}),等待 5 秒'.format('air.jp.co.cygames.worldflipper'))
        self.ADB.Shut_Down_Game(
            Game_Activity_Name='air.jp.co.cygames.worldflipper')
        time.sleep(5)

    def Delete_Record(self):
        logging.info('為了刪除紀錄,開始判斷是否進入遊戲主頁面')
        print('為了刪除紀錄,開始判斷是否進入遊戲主頁面(透過右上角button)')
        self.Check_Stat(mode='delete-button')
        logging.info('已進入遊戲主頁面,開始刪除紀錄')
        print('開始刪除紀錄')
        self.ADB.Touch(500, 56)  ##右上角選單
        time.sleep(2)
        self.ADB.Touch(392, 482)  ##清除紀錄選項(垃圾桶)
        time.sleep(2)
        self.ADB.Touch(391, 693)  ## OK  12/14更新位置
        #self.ADB.Touch(395,635)  ## OK (red-button)   old
        time.sleep(1)
        self.ADB.Touch(395, 635)  ## OK_double-check (red-button)
        time.sleep(8)
        ##通信中,等待遊戲重啟
        #function 判斷是否到遊戲啟動首頁
        ## goto login_game

    def Login_Game(self, achiveTime='firstTime'):
        logging.info('開始判斷是否進入遊戲主頁面')
        print('開始判斷是否進入遊戲主頁面(透過右上角button)')
        self.Check_Stat(mode='delete-button')
        logging.info('已進入遊戲主頁面,點擊螢幕進入遊戲')
        print('已進入遊戲主頁面,點擊螢幕進入遊戲')
        self.ADB.Touch(395, 635)  ##點擊螢幕
        if achiveTime == 'firstTime':
            logging.info('新帳號第一次進入遊戲,開始判斷是否出現同意')
            print('新帳號第一次進入遊戲,開始判斷是否出現同意')
            self.Check_Stat(mode='agree-button')
            logging.info('已出現,點擊同意')
            print('已出現,點擊同意')
            self.ADB.Touch(395, 635)  ##同意
            logging.info('開始判斷是否是否出現新手教學')
            print('開始判斷是否是否出現新手教學')
            self.Check_Stat(mode='canceltutorial-button')
            logging.info('已出現,點擊取消')
            print('已出現,點擊取消')
            self.ADB.Touch(151, 623)  ##取消 (red-button)
            time.sleep(2)
            self.ADB.Touch(151, 623)  ##取消 again(red-button)
            time.sleep(2)
            self.ADB.Touch(273, 803)  ##ok again(red-button)
            time.sleep(2)
            for i in range(5):
                self.ADB.Touch(385, 901)  ##下一步x5
                time.sleep(0.8)
            logging.info('開始判斷是否是否出現取名ok')
            print('開始判斷是否是否出現取名ok')
            self.Check_Stat(mode='nameconfirm-button')  ##判斷是否出現取名ok
            logging.info('已出現,確認取名')
            print('已出現,確認取名')
            self.ADB.Touch(269, 465)  ##ok (green-button)
            time.sleep(1.5)
            self.ADB.Touch(395, 635)  ## OK (red-button)
        ##通信中
        ##function 判斷是否出現抽卡區
        time.sleep(2)
        ## goto summoning

    def Summoning(self):
        logging.info('開始判斷是否是否找到抽卡區')
        print('開始判斷是否是否找到抽卡區')  #這邊太慢
        self.ADB.Image_Grab(mode='get_summon-button')
        stat = self.ADB.Recognize_Img(mode='summon-button')
        while stat != True:
            self.ADB.Image_Grab(mode='get_summon-button')
            stat = self.ADB.Recognize_Img(mode='summon-button')
        logging.info('已看到抽卡區,點擊進入抽卡區')
        print('已看到抽卡區,點擊進入抽卡區')
        self.ADB.Touch(407, 933)  ##進入抽卡區
        time.sleep(1.5)
        self.ADB.Touch(233, 663)  ##進行強制單抽  (保底四星,有機會出五星)
        time.sleep(1.5)
        self.ADB.Touch(395, 635)  ## OK (red-button)
        logging.info('開始判斷是否找到skip按鈕')
        print('開始判斷是否找到skip按鈕')
        self.Check_Stat(mode='skip-button')
        logging.info('已找到,按下skip')
        print('已找到,按下skip')
        self.ADB.Touch(529, 25)  #skip
        logging.info('開始判斷是否是否抽完了')
        print('開始判斷是否是否抽完了')
        self.ADB.Image_Grab(mode='get_got4starconfirm-button')
        stat = self.ADB.Recognize_Img(mode='got4starconfirm-button')
        while stat != True:
            self.ADB.Touch(529, 25)
            self.ADB.Image_Grab(mode='get_got4starconfirm-button')
            stat = self.ADB.Recognize_Img(mode='got4starconfirm-button')
        logging.info('已抽完,按下OK確認')
        print('已抽完,按下OK確認')
        time.sleep(1)
        self.ADB.Touch(272, 620)  ##OK
        time.sleep(1)
        self.ADB.Touch(476, 833)  ##回上一頁
        ##讀取中
        logging.info('開始判斷是否是否出現送禮')
        print('開始判斷是否是否出現送禮')
        self.ADB.Image_Grab(mode='get_gotpriceconfirm-button')
        stat = self.ADB.Recognize_Img(mode='gotpriceconfirm-button')
        while stat != True:  #判斷是否出現送禮
            self.ADB.Touch(529, 25)  #連點
            self.ADB.Image_Grab(mode='get_gotpriceconfirm-button')
            stat = self.ADB.Recognize_Img(mode='gotpriceconfirm-button')
        logging.info('已收到送禮,按下接收')
        print('已收到送禮,按下接收')
        time.sleep(1)
        self.ADB.Touch(351, 641)  ##接收禮物按鈕
        logging.info('開始判斷是否是否出現ok')
        print('開始判斷是否是否出現ok')
        self.ADB.Image_Grab(mode='get_afterpriceconfirmok-button')
        stat = self.ADB.Recognize_Img(mode='afterpriceconfirmok-button')
        while stat != True:  ##判斷是否出現ok
            self.ADB.Touch(529, 25)  #連點
            self.ADB.Image_Grab(mode='get_afterpriceconfirmok-button')
            stat = self.ADB.Recognize_Img(mode='afterpriceconfirmok-button')
        logging.info('已出現,按下OK確認')
        print('已出現,按下OK確認')
        time.sleep(1)
        self.ADB.Touch(351, 641)  ##OK
        time.sleep(1.5)
        logging.info('進行十連抽')
        self.ADB.Touch(351, 641)  ##十連抽
        time.sleep(1.5)
        self.ADB.Touch(351, 641)  ##十連抽-確定
        logging.info('開始判斷是否找到skip按鈕')
        print('開始判斷是否找到skip按鈕')
        self.Check_Stat(mode='skip-button')
        logging.info('已找到,按下skip')
        print('已找到,按下skip')
        self.ADB.Touch(529, 25)  #skip
        time.sleep(1)
        ## force-stop Game

    def Check_Box(self):
        logging.info('判斷是否出現包包選項')
        print('開始判斷是否出現包包選項')
        self.ADB.Image_Grab(mode='get_member-button')
        stat = self.ADB.Recognize_Img(mode='member-button')
        while stat != True:  ##function 判斷是否出現包包
            #self.ADB.Touch(529,25
            self.ADB.Touch(229, 928)
            self.ADB.Image_Grab(mode='get_member-button')
            stat = self.ADB.Recognize_Img(mode='member-button')
        logging.info('已出現,打開包包')
        print('已出現,打開包包')
        time.sleep(1)
        self.ADB.Touch(495, 105)  ##打開包包
        time.sleep(4)
        ## goto star Analysis

    def Star_Analysis(self):
        logging.info('擷取包包內照片')
        print('開始擷取包包內照片')
        self.ADB.Image_Grab(mode='get_star')
        logging.info('判斷角色星數')
        print('開始判斷角色星數')
        starNumber = self.ADB.Recognize_Img(mode='star')
        self.ADB.Touch(35, 921)  ##離開角色包包
        if starNumber >= 2:
            logging.info('得到結果,獲得五星角色 {} 隻,開始截圖保存至指定目錄'.format(starNumber))
            print('得到結果,獲得五星角色 {} 隻,開始截圖保存至指定目錄'.format(starNumber))
            self.ADB.Game_ScreenHot_By_Adb(
                save_path='./image/account/waitForAnalysis.jpg')
        return starNumber

    def Get_Account_ID(self):
        logging.info('判斷是否已回到首頁')
        print('開始判斷是否已回到首頁')
        self.ADB.Image_Grab(mode='get_summon-button')
        stat = self.ADB.Recognize_Img(mode='summon-button')
        while stat != True:
            self.ADB.Touch(35, 921)  ##上一頁
            self.ADB.Image_Grab(mode='get_summon-button')
            stat = self.ADB.Recognize_Img(mode='summon-button')
        logging.info('已回到首頁,進入\'其他\'選單')
        print('已回到首頁,進入\'其他\'選單')
        self.ADB.Touch(492, 921)  #進入'其他'選單
        time.sleep(1.5)
        self.ADB.Touch(267, 851)  #點擊引繼
        logging.info('判斷是否已進到準備引繼的選單')
        print('開始判斷是否已進到準備引繼的選單')
        self.ADB.Image_Grab(mode='get_setaccount-button')
        stat = self.ADB.Recognize_Img(mode='setaccount-button')
        while stat != True:
            self.ADB.Image_Grab(mode='get_setaccount-button')
            stat = self.ADB.Recognize_Img(mode='setaccount-button')
        logging.info('已進入,開始引繼')
        print('已進入,開始引繼')
        time.sleep(0.5)
        self.ADB.Touch(265, 583)  #點擊開始引繼
        time.sleep(1)
        self.ADB.Touch(388, 816)  #ok
        time.sleep(1)
        self.ADB.Touch(266, 490)  #點擊使用引繼碼
        time.sleep(1)
        self.ADB.Touch(270, 659)  #ok
        time.sleep(1)
        self.ADB.Touch(263, 566)  #點擊生成引繼碼
        time.sleep(1)
        self.ADB.Touch(261, 354)  #點擊密碼框
        time.sleep(0.5)
        self.ADB.Text_Input('4700worldFlipper')
        time.sleep(0.5)
        self.ADB.Touch(261, 413)  #點擊確認密碼框
        time.sleep(0.5)
        self.ADB.Text_Input('4700worldFlipper')
        time.sleep(0.5)
        self.ADB.Touch(70, 597)  #勾選同意條款
        time.sleep(0.5)
        self.ADB.Touch(268, 700)  #下一步
        time.sleep(1)
        #通信中

    def Recognize_Account(self, starNumber=0):
        logging.info('判斷是否已引繼成功得到引繼碼')
        print('開始判斷是否已引繼成功得到引繼碼')
        self.ADB.Image_Grab(mode='get_accountconfirm-button')
        stat = self.ADB.Recognize_Img(mode='accountconfirm-button')
        while stat != True:
            self.ADB.Image_Grab(mode='get_accountconfirm-button')
            stat = self.ADB.Recognize_Img(mode='accountconfirm-button')
        account = self.ADB.Image_Grab(mode='get_account')
        logging.info('成功獲得。將獲得的引繼碼({})存入指定目錄'.format(account))
        print('開始將獲得的引繼碼({})存入指定目錄'.format(account))
        os.rename(
            './image/account/waitForAnalysis.jpg',
            './image/account/{}_fiveStar/{}.jpg'.format(starNumber, account))

    def Check_Stat(self, mode=None):
        time.sleep(3)
        count = 0
        self.ADB.Image_Grab(mode=('get_' + mode))
        stat = self.ADB.Recognize_Img(mode=mode)
        while stat != True:
            if mode == 'delete-button' and count > 60:
                logging.error('當機!關閉遊戲並等待 3 秒後重啟')
                self.ADB.Start_Game(Game_Activity_Name=
                                    'air.jp.co.cygames.worldflipper/.AppEntry')
                time.sleep(3)
            #print('目前狀態: ', stat)
            count = count + 1
            self.ADB.Image_Grab(mode=('get_' + mode))
            stat = self.ADB.Recognize_Img(mode=mode)
        #time.sleep(0.3)

    ########################################################################
    ###
    ###    自動開房
    ###
    ########################################################################
    def Selete_Boss(self, boss_Number=0):
        logging.info('判斷是否已到boss選單')
        print('開始判斷是否已到boss選單')
        self.Check_Stat(mode='87snake-button')
        time.sleep(1)
        logging.info('已進入boss選單主頁面,開始開房')
        print('已進入boss選單主頁面,開始開房')
        if boss_Number == 0:
            self.ADB.Touch(79, 449)  ##大蛇
        if boss_Number == 1:
            self.ADB.Touch(82, 549)  ##光上
        if boss_Number == 2:
            self.ADB.Touch(88, 666)  ##白虎
        if boss_Number == 3:
            self.ADB.Touch(82, 763)  ##暗上
        if boss_Number == 4:
            self.ADB.Touch(74, 859)  ##水上
        if boss_Number == 5:
            self.ADB.Swipe(273, 845, 273, 214)
            time.sleep(0.4)
            self.ADB.Touch(273, 586)  ##火上
        if boss_Number == 6:
            self.ADB.Swipe(273, 845, 273, 214)
            time.sleep(0.4)
            self.ADB.Touch(273, 703)  ##不死
        if boss_Number == 7:
            self.ADB.Swipe(273, 845, 273, 214)
            time.sleep(0.4)
            self.ADB.Touch(273, 808)  ##貓頭鷹
        time.sleep(1)
        self.ADB.Touch(269, 351)
        time.sleep(1)
        self.ADB.Touch(278, 713)
        time.sleep(1)
        pass

    def Check_DoudlePrice(self):
        pass

    def Check_MultiPlay(self):
        pass

    def Check_SecondMenber(self, party_Member=0):
        logging.info('判斷是否進入第二名成員')
        print('判斷是否進入第二名成員')
        self.ADB.Image_Grab(mode='get_roomstatSecondMenberStat-button')
        stat = self.ADB.Recognize_Img(mode='roomstatSecondMenberStat-button')
        while stat != False:
            roomStat = self.Is_Dismissed()
            if roomStat == True:
                logging.info('房間已被解散')
                print('房間已被解散')
                return 'dismissed'
            self.ADB.Image_Grab(mode='get_roomstatSecondMenberStat-button')
            stat = self.ADB.Recognize_Img(
                mode='roomstatSecondMenberStat-button')
        logging.info('已有第二名成員')
        print('已有第二名成員')
        if party_Member == 2:
            logging.info('已滿人,出發')
            print('已滿人,出發')
            time.sleep(1)
            self.ADB.Touch(398, 629)
            time.sleep(1)
            return 'party is full!'
            pass
        time.sleep(1)
        return 'second member joined!'
        pass

    def Is_FullPayty(self):
        logging.info('判斷是否滿人')
        print('判斷是否滿人')
        self.ADB.Image_Grab(mode='get_roomstat-button')
        stat = self.ADB.Recognize_Img(mode='roomstat-button')
        while stat != False:
            roomStat = self.Is_Dismissed()
            if roomStat == True:
                logging.info('房間已被解散')
                print('房間已被解散')
                return 'dismissed'
            self.ADB.Image_Grab(mode='get_roomstat-button')
            stat = self.ADB.Recognize_Img(mode='roomstat-button')
        logging.info('已滿人,出發')
        print('已滿人,出發')
        time.sleep(1)
        self.ADB.Touch(398, 629)
        time.sleep(1)
        return 'party is full!'
        pass

    def Is_RoomCreated(self, party_Member=0):
        logging.info('判斷是否已開啟房間')
        print('判斷是否已開啟房間')
        self.Check_Stat(mode='roomstat-button')
        logging.info('已建立房間')
        print('已建立房間')
        time.sleep(1)
        self.ADB.Touch(73, 628)  ##按下準備
        time.sleep(0.6)
        self.ADB.Touch(270, 708)  ##按下招募
        time.sleep(0.6)
        self.ADB.Touch(256, 402)  ##按下招募好友
        if party_Member != 2:
            time.sleep(0.6)
            self.ADB.Touch(270, 708)  ##按下招募-第二次
            time.sleep(0.6)
            self.ADB.Touch(278, 490)  ##按下招募鈴鐺
        time.sleep(1)
        pass

    def Is_Dismissed(self):
        logging.info('判斷是否被解散房間')
        print('判斷是否被解散房間')
        self.ADB.Image_Grab(mode='get_dismissed-button')
        stat = self.ADB.Recognize_Img(mode='dismissed-button')
        return stat  ##ok button in 268,620
        pass

    def Check_In_Boss_Fight(self):
        logging.info('判斷是否已開始戰鬥')
        print('判斷是否已開始戰鬥')
        self.ADB.Image_Grab(mode='get_bossFightStat-button')
        stat = self.ADB.Recognize_Img(mode='bossFightStat-button')
        while stat != True:
            roomStat = self.Is_Dismissed()
            if roomStat == True:
                logging.info('房間已被解散')
                print('房間已被解散')
                return 'dismissed'
            self.ADB.Touch(398, 629)
            self.ADB.Image_Grab(mode='get_bossFightStat-button')
            stat = self.ADB.Recognize_Img(mode='bossFightStat-button')
        logging.info('已開始戰鬥,執行退出戰鬥')
        print('已開始戰鬥,執行退出戰鬥')
        time.sleep(1)
        self.ADB.Touch(33, 54)
        time.sleep(1)
        self.ADB.Touch(25, 929)
        time.sleep(1)
        self.ADB.Touch(399, 622)
        time.sleep(1)
        return 'go back to menu'
        pass
class Controller:
    def __init__(self):
        self.ADB = ADB(device_Name=os.environ.get('DEVICE_NAME'),
                       screen_Size=(560, 960))
        self.package = os.environ.get('PACKAGE_NAME')
        self.activity = os.environ.get('ACTIVITY_NAME')

    def Game_Start(self):
        LOG.info(f'啟動遊戲({self.game_package}),等待 5 秒')
        self.ADB.Start_Game(
            Game_Activity_Name=f'{self.package}/{self.activity}')
        time.sleep(5)

    def Game_Stop(self):
        LOG.info(f'關閉遊戲({self.game_package}),等待 5 秒')
        self.ADB.Shut_Down_Game(Game_Activity_Name=f'{self.package}')
        time.sleep(5)

    def Login_Game(self):
        pass

    def Get_Account_ID(self):
        logging.info('判斷是否已回到首頁')
        print('開始判斷是否已回到首頁')
        self.ADB.Image_Grab(mode='grabed_summon-button')
        stat = self.ADB.Recognize_Img(mode='summon-button')
        while stat != True:
            self.ADB.Touch(35, 921)  ##上一頁
            self.ADB.Image_Grab(mode='grabed_summon-button')
            stat = self.ADB.Recognize_Img(mode='summon-button')
        logging.info('已回到首頁,進入\'其他\'選單')
        print('已回到首頁,進入\'其他\'選單')
        self.ADB.Touch(492, 921)  #進入'其他'選單
        time.sleep(1.5)
        self.ADB.Touch(267, 851)  #點擊引繼
        logging.info('判斷是否已進到準備引繼的選單')
        print('開始判斷是否已進到準備引繼的選單')
        self.ADB.Image_Grab(mode='grabed_setaccount-button')
        stat = self.ADB.Recognize_Img(mode='setaccount-button')
        while stat != True:
            self.ADB.Image_Grab(mode='grabed_setaccount-button')
            stat = self.ADB.Recognize_Img(mode='setaccount-button')
        logging.info('已進入,開始引繼')
        print('已進入,開始引繼')
        time.sleep(0.5)
        self.ADB.Touch(265, 583)  #點擊開始引繼
        time.sleep(1)
        self.ADB.Touch(388, 816)  #ok
        time.sleep(1)
        self.ADB.Touch(266, 490)  #點擊使用引繼碼
        time.sleep(1)
        self.ADB.Touch(270, 659)  #ok
        time.sleep(1)
        self.ADB.Touch(263, 566)  #點擊生成引繼碼
        time.sleep(1)
        self.ADB.Touch(261, 354)  #點擊密碼框
        time.sleep(0.5)
        self.ADB.Text_Input('47004700')
        time.sleep(0.5)
        self.ADB.Touch(261, 413)  #點擊確認密碼框
        time.sleep(0.5)
        self.ADB.Text_Input('47004700')
        time.sleep(0.5)
        self.ADB.Touch(70, 597)  #勾選同意條款
        time.sleep(0.5)
        self.ADB.Touch(268, 700)  #下一步
        time.sleep(1)
        #通信中

    def Recognize_Account(self, starNumber=0):
        logging.info('判斷是否已引繼成功得到引繼碼')
        print('開始判斷是否已引繼成功得到引繼碼')
        self.ADB.Image_Grab(mode='get_accountconfirm-button')
        stat = self.ADB.Recognize_Img(mode='accountconfirm-button')
        while stat != True:
            self.ADB.Image_Grab(mode='get_accountconfirm-button')
            stat = self.ADB.Recognize_Img(mode='accountconfirm-button')
        account = self.ADB.Image_Grab(mode='get_account')
        logging.info('成功獲得。將獲得的引繼碼({})存入指定目錄'.format(account))
        print('開始將獲得的引繼碼({})存入指定目錄'.format(account))
        os.rename(
            './image/account/waitForAnalysis.jpg',
            './image/account/{}_fiveStar/{}.jpg'.format(starNumber, account))

    def check(self, mode=None):
        time.sleep(3)
        count = 0
        self.ADB.Image_Grab(mode=f'grabed_{mode}')
        stat = self.ADB.Recognize_Img(mode=mode)
        while stat != True:
            if mode == 'delete-button' and count > 60:
                logging.error('當機!關閉遊戲並等待 3 秒後重啟')
                self.ADB.Start_Game(
                    Game_Activity_Name=f'{self.package}/{self.activity}')
                time.sleep(3)
            #print('目前狀態: ', stat)
            count = count + 1
            self.ADB.Image_Grab(mode=mode)
            stat = self.ADB.Recognize_Img(mode=mode)
        #time.sleep(0.3)

    ##position coordinate---
    ##318,458,443,485 account's
    ##470,78,519,110 team-member's
    ##370,900,442,951 summon-button's
    ##158,582,386,609 setaccount-button
    ##470,26,512,67  delete-button
    ##340,642,437,668  agree-button
    ##111,606,182,629  canceltutorial-button
    ##239,607,291,628  got4starconfirm-button
    ##245,457,290,478  nameconfirm-button
    ##244,663,287,682  accountconfirm-button
    ##176,394,361,481  gotpriceconfirm-button
    ##248,608,287,629  afterpriceconfirmok-button
    ##460,26,503,44  skip-button
    ##434,803,511,854  backtotop-button
    ##----- star(差距66,16)  每一行間角色間距114
    ##79,452,145,468 [0,0]star  193,452,259,468 [0,1]star  307,452,373,468 [0,2]star  421,452,487,468 [0,3]star   1,2,3,4
    ##135,602,201,618 [1,0]star 249,602,315,618 [1,1]star  363,602,429,618 [1,2]star                              5,6,7
    ##79,752,145,768 [2,0]star  193,752,259,768 [2,1]star  307,752,373,768 [2,2]star  451,752,487,768 [2,3]star   8,9,10,11
    ##135,902,201,918 [3,0]star 249,902,315,902 [3,1]star  363,902,429,902 [3,2]star                              12,13,1

    # TODO: ImageGrabu應該只做一件事, 判斷做什麼應該放在controller內, return img(cv2.imread)即可
    def Image_Grab(self, mode=None, coordinate=[0, 5, 0, 5]):
        tmp_grab_path = r'/image/openCV_Img/'
        path_ = tmp_grab_path + 'tmp_grab.jpg'
        self.Game_ScreenHot_By_Adb(save_path=path_)
        img = cv2.imread('.' + path_, cv2.IMREAD_GRAYSCALE)
        if mode == 'check_star':
            self.Grab_Charactor_Star(img=img)
        elif mode:
            self.Grab_Screen_Partition(img=img,
                                       mode=mode,
                                       coordinate=coordinate)

    def Grab_Screen_Partition(self, img=img, mode=None, coordinate=coordinate):
        tmp_grab_path = r'/image/openCV_Img/'
        if mode == 'account':
            pytesseract.pytesseract.tesseract_cmd = r"C:/Program Files/Tesseract-OCR/tesseract.exe"
            imgs = img[458:485, 318:443]  ##[y1::y2, x1:x2]
            img_np = np.array(imgs)
            frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB)
            cv2.imwrite('.' + tmp_grab_path + mode + '.jpg', frame)
            result = pytesseract.image_to_string(frame)
            result = re.sub(r'[l\[\]I]', '1', result)
            return result
        elif mode == 'summon-button':  ## summon-button
            imgs = img[900:951, 370:442]
        elif mode == 'member-button':  ## member-button
            imgs = img[78:110, 470:519]
        elif mode == 'setaccount-button':  ##158,582,386,609 setaccount-button
            imgs = img[582:609, 158:386]
        elif mode == 'delete-button':  ##470,26,512,67  delete-button
            imgs = img[26:67, 470:512]
        elif mode == 'agree-button':  ##340,642,437,668  agree-button
            imgs = img[642:668, 340:437]
        img_np = np.array(imgs)
        frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB)
        cv2.imwrite('.' + tmp_grab_path + 'checkPoint.jpg', frame)
        if mode is not None and coordinate != [0, 5, 0, 5]:
            imgs = img[coordinate[1]:coordinate[3],
                       coordinate[0]:coordinate[2]]
            img_np = np.array(imgs)
            frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB)
            cv2.imwrite('.' + tmp_grab_path + mode + '.jpg', frame)

    def Grab_Charactor_Star(self, img):
        coordinate = [[79, 452, 145, 468], [193, 452, 259, 468],
                      [307, 452, 373, 468], [421, 452, 487, 468],
                      [136, 602, 202, 618], [250, 602, 316, 618],
                      [364, 602, 430, 618], [79, 752, 145, 768],
                      [193, 752, 259, 768], [307, 752, 373, 768],
                      [421, 752, 487, 768], [136, 902, 202, 918],
                      [250, 902, 316, 918], [364, 902, 430, 918]]
        tmp_grab_path = r'/image/openCV_Img/'
        for i, star in enumerate(coordinate, 1):
            #print('擷取第{}隻'.format(i))
            imgs = img[star[1]:star[3], star[0]:star[2]]
            img_np = np.array(imgs)
            frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB)
            cv2.imwrite(f'.{tmp_grab_path}{i}.jpg', frame)
        time.sleep(1)

    # TODO: 此function應該放在controller內, 上下兩段也應該拆開寫
    def Recognize_Img(self, mode='None'):
        img_Path = r'./image/openCV_Img/'
        if mode == 'star':
            LOG.info('判斷五星角色數量')
            print('開始判斷五星角色數量')
            img_Sample = cv2.imread(img_Path + 'five_Star11.png', 0)
            starNumber = 0
            for i in range(1, 15):
                img_Compare = cv2.imread(img_Path + str(i) + '.jpg', 0)
                score = compare_ssim(img_Sample, img_Compare)
                #LOG.info('第 {} 隻為五星的相似度為 {}%'.format(i,round(score*100,2)))
                #print('第 {} 隻為五星的相似度為 {}%'.format(i,round(score*100,2)))
                if score > 0.85:
                    starNumber = starNumber + 1
            LOG.info('總共抽到 {} 隻五星角色'.format(starNumber))
            print('總共抽到 {} 隻五星角色'.format(starNumber))
            return starNumber

        img_Sample = cv2.imread(img_Path + mode + '.png', 0)
        if img_Sample is None:
            img_Sample = cv2.imread(img_Path + mode + '.jpg', 0)
        img_Compare = cv2.imread(img_Path + 'checkPoint' + '.jpg', 0)
        #img_Compare=cv2.resize(img_Compare,img_Sample.shape)
        (H, W) = img_Sample.shape
        # to resize and set the new width and height
        img_Compare = cv2.resize(img_Compare, (W, H))
        score = compare_ssim(img_Sample, img_Compare)
        if score >= 0.95:
            score = round(score * 100, 2)
            LOG.info('相似度: {}%, 判定成功'.format(score))
            print('相似度: {}%, 判定成功'.format(score))
            return True
        return False