from ctypes import Union
import numpy as np
import random

class make_case(object):

    def __init__(
        self
    ):
        self = 0

    def data_gen(
            self,
            mode        = 'random',
            length      = 1,
            params      = {}
    ):

        match mode:
            case 'random':
                data = [random.randint(0,2**32) for _ in range(length)]
            case 'ones':
                data = (2**32-1)*np.ones(length)
            case 'amp':
                amp = params['amp']
                amp = int(amp,0) if isinstance(amp, str) else int(amp)
                data = amp*np.ones(length)
            case 'zeros':
                data = np.zeros(length)
            case 'value':
                value = params['value']
                if np.size(value) != length:
                    print("Warnning: Length Mismatch")
                elif length ==1:
                    data = int(value,0) if isinstance(value, str) else int(value)
                else:
                    data = np.zeros(length)
                    for i in range(0,length):
                        data[i] = int(value[i],0) if isinstance(value[i], str) else int(value[i])
            case 'acc':
                ini_data = params['ini_data']
                ini_data = int(ini_data,0) if isinstance(ini_data, str) else int(ini_data)
                step_size = params['step_size']
                step_size = int(step_size,0) if isinstance(step_size, str) else int(step_size)
                data = np.zeros(length)
                for i in range(0,length):
                    data[i] = ini_data + i*step_size
            case 'rd_file':
                file_name = params['file_name']
                with open(file_name, "r") as f:
                    data_bin = f.read()
                data_bin = data_bin.split('\n')
                data = []
                for d in data_bin:
                    data.append((int(d,2)))
        return data
    
    def rw_once(
            self,
            op          = 'w', 
            addr        = 0x1F00044, 
            data        = [0], 
            file_name   = 'case.txt',
            chip_id     = 0,
            exaddr      = 1,
            ard_flag    = 0
    ):

        with open(file_name, "a") as f:
            cmd = 1 if (op=='r') or (op==1) else 0
            if isinstance(addr, str):
                addr = int(addr,0)
            else:
                addr = int(addr)
            f.write(f"{((int(cmd)<<31) | (int(ard_flag)<<30) | (int(chip_id)<<25) | (addr)):08x}\n")
            f.write(f"{((int(exaddr)<<20) | (int(np.size(data)*4))):08x}\n")

            if op == 'w':
                if np.size(data) == 1:
                    if isinstance(data, str):
                        dt = int(data,0)
                    else:
                        dt = int(np.round(data))
                    f.write(f"{(dt if dt>=0 else 2**32+dt):08x}\n")
                else:
                    for i in range(0,np.size(data)):
                        if isinstance(data[i], str):
                            dt = int(data[i],0)
                        else:
                            dt = int(np.round(data[i]))
                        f.write(f"{(dt if dt>=0 else 2**32+dt):08x}\n")
            f.write('\n')     

