from typing import Union

import pandas as pd
from PyQt5.QtWidgets import QWidget

from base.base import (
    BaseMediator, 
    BaseFileManager, 
    BaseDataConverter, 
    BasePlotWidget, 
    BasePointPassportFormer, 
    BaseController, 
    GraphicPassport,
    Settings,
    BaseRawTraceProcessor
    )


class Mediator(BaseMediator):

    def notify(self,
               source: Union[BaseFileManager, BaseDataConverter, BasePointPassportFormer, BasePlotWidget, BaseController, BaseRawTraceProcessor],
               data: Union[list[str], list[pd.DataFrame], list[GraphicPassport], list[QWidget], Settings, pd.DataFrame]) -> None:
        
        if issubclass(source.__class__, BaseFileManager):
            self._controller.update_status("CSV found! Calculating...")
            self._converter.convert_data(data)

        if issubclass(source.__class__, BaseDataConverter):
            self._controller.update_progress(1)
            self._passport_former.form_passports(data)

        if issubclass(source.__class__, BasePointPassportFormer):
            self._controller.update_progress(10)
            self._plot.build(data)

        if issubclass(source.__class__, BasePlotWidget):
            self._controller.update_progress(100)
            self._controller.send_widgets(data)
        
        if issubclass(source.__class__, BaseController):
            self._file_manager.update_monitor_settings(data)
            self._passport_former.update_settings(data)
            self._trace_processor.update_settings(data)
    
    def prerender_TCW(self, source:Union[BaseController, BaseFileManager, BaseRawTraceProcessor], 
                  data:Union[str, list[str], list[pd.DataFrame, dict]]) -> None:
        
        if issubclass(source.__class__, BaseController):
            self._controller.update_progress(5)
            self._file_manager.open_raw_traces_dir(data)
        
        if issubclass(source.__class__, BaseFileManager):
            self._controller.update_progress(15)
            self._trace_processor.prerender(data)

        if issubclass(source.__class__, BaseRawTraceProcessor):
            self._controller.update_progress(40)
            self._plot.build_raw_trace(data)

    def render_TCW(self, source:Union[BaseController, BaseRawTraceProcessor], 
                  data:Union[str, list[pd.DataFrame, dict]] = None) -> None:
        
        if issubclass(source.__class__, BaseController):
            self._controller.update_progress(5)
            self._trace_processor.final_render()

        if issubclass(source.__class__, BaseRawTraceProcessor):
            self._controller.update_progress(5)
            self._passport_former.form_customer_passport(data)
            
    def set_mode(self, mode: int) -> None:
        self._plot.set_mode(mode)
        self._file_manager.set_mode(mode)



