import sys
import os
import random
import cProfile

import pandas as pd
from tqdm import tqdm

from giftwrap.utils import VisiumHDFormatInfo

def main():
    # Example read from a Visium HD dataset (with an error)
    reads="""TCCGTTCAAGGAACAGTAGCTCGTCTGGCATGTGCGCCTTTT
TTCACTGAGGATGCATAGTGGATGTCTTGTGGATGTAGGGTT
GAGTGGGCTGTCACATGAGGCCATGTATGGCAAATCATCGGC
AAGGGTTTAGAGTTGTCGCACAGATCGTTGCAGCGCGCAGTC
CAGCCAGCTGTTATCAGCGCAGCAGACAACAGCATCGCGTTT
ATCCATGTAGTCCACAGCTCAATAGCCAGCCTGGCACGAGTT
CAAGCGCCGGTTGCAGCCGCTGTCGCCAGCAGACATACGGAT
CAAGCGCCGGTTGCAGCCGCTGTCGCCAGCAGACATACGGAT
TGCTGGGGGGTTGTGTCTGATGTCAGCAGTGTGCAAGATCGA
AAGATATGAGAGCTGAGAGATGCTGTTGCCGATAGAAGGGTT
GTCTTTGTGGTTCACATATCTCAGTGCCACTCGAATTTTTTT
TAGTCATAAACGTCTCCACAGCGCCGTGTGATCAGCTTGGCT
GCTGAGTTCACGTAGCCGCGCTGCCAGTGCTCGCCTGATAGG
AGTGGCAATGTTCTGCGTGATATAGCTCTGCCATGCACGCAT
TATATATTAAGTAGATATTGGCATGTTGAGCACTCAGCTGAG
AGCCTAGAGGCTGTCTGACAGCAGTATTGCGTGGTAAGATTT
ATTCTGGGCTAGTCATGTGCGCTTGCTGCTGATGATGACCAG
TATGCTGATGTCTATCTGCCTACAGTCTCTCAGCATGCATGG
GTAGGCCGAGCAGAGCTGCCAGCTGTCTCTATGCACTCCAGG
GTTTGAAATGTAGCTCTCATAAGCGCTTCTGCACATGGGTTT
TGTGCGATGGCGTATCTCTGTCGAAGACACGCTGTGTAGCAT
TCCTTGACCGTATGCGCTGAAAAGTAGAGCTCAGAATCGACT
TTGAAATGGGCACTCTTCTGACGCATAGTGAATCCAGAGTTT
TAGAGGGTTGCAATTGGCTCTCGACTCTATCTGACGACTTTT
GACCTATATACGACCAGCATTCAACAGTACCTGCATGAGAAG
CAGAGCACGCGATCTGTCACTCACAGTCAGCATCGGCACTAG
AGGCTTCCAGCATCCTGTGCTCTCGTCACAGCGCTGAGAAGG
TAGAGGGTTGCAATTGGCTCTCGACTCTATCTGACGACTTTT
ATCTGTTGCAGTCTTTTGCGCATCGTCAGCTCAGTCTCGGGT
CTCTTCTTTGGGTCTGGTGTTGACAGTCGCTGAGGCGCAGGA
TATGCACAGCGTGCACAGCACATGGCACAGCTGGGTTTTTTT
CGTGAATGCGTCCGCTCTGTGCCGCTGCTGTGGAGATAGAGT
CTGGCCACGGCACTCGTCATGACGTATTGAACAAGCATTTTT
GGGGGGCCGTGTCATGCATATAACGTCTCATTGCAATCACGA
TTATGCTAGTGATATGTCAGCACTGTCGCTCATTTGATGAAT
TAAATTAGAGATAGCTGATGAGAAGATGTAAGACTCAGGCTT
ATCGGCAACGATGTGTGCTGAACAGTAGCATTCGTCATGCGT
GGTGATGATCGCTGTGCATGAAGCATGACATCATGCCGAGTT
ATACACCTCGTCAGTGGATATTCAGTCTTAGCAGCTTCAGCA
GAGTGAGCCGTTGATATGTCACAGTCGCAATGCACGAATTTT
TATGAGAGGATCAGATGTCCAACATAGGGTTTTTTTTTTTTT
GCTTCAATGGTGATCATGACAGTCGCATGGATTCACATGGCT
ATCTTGGTAGGTCGATCTGCAGCCTCTCATGCATCTTGGCTT
TAATGACTTGAGCTGAGTTGCGCTAGCTGAGAGATTGGGTTT
TATACTCACGGCATATCTATGATCGCTCTGCATATCGCAGAA
GGTAGTCAAGATATCTGAGGCAAGCAGATCCGAGCACGGCTT""".splitlines()
    read=reads[1]
    # # Generate random reads
    # read_len = len(read)
    # N_fake_reads = 100//2
    # reads = []
    # for _ in range(N_fake_reads):
    #     # Generate a random read of the same length
    #     random_read = ''.join(random.choice('ACGT') for _ in range(read_len))
    #     reads.append(random_read)

    tech_info = VisiumHDFormatInfo(
        space_ranger_path="/Users/austinv11/PycharmProjects/giftwrap/spaceranger-4.0.1/bin/"
    )

    # for r in reads:
    #     bc, was_corrected = tech_info.correct_barcode(
    #         r,
    #         4,
    #         start_idx=0,
    #         end_idx=len(r),
    #     )
    #'AGGATAAAAGGTTCTGGCATGTGCCGTCATGCATGCCGTAGGGT'
             #['GTTCTGGCATGTGCCGTACTGCATGCCGTAG']
    mapped_bc= 'GTTCTGGCATGTGCCGTCATGCATGGTTATG'
    correct_cell="s_002um_00571_01229-1"
    bc, corrections = tech_info.correct_barcode(
        read,
        4,
        start_idx=0,
        end_idx=len(read),
    )
    if bc is None:
        print("Correction failed, no valid barcode found.")
        return
    coordinate_x, coordinate_y = tech_info.barcode2coordinates(bc)
    converted = tech_info.make_barcode_string(bc, x_coord=coordinate_x, y_coord=coordinate_y, is_multiplexed=False)
    print(converted == correct_cell, converted, correct_cell)
    
if __name__ == '__main__':
    main()
