import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { action, computed, observable, runInAction } from 'mobx';
import { observer } from 'mobx-react';
import * as React from 'react';
import { ColorState, SketchPicker } from 'react-color';
import { Doc } from '../../fields/Doc';
import { Id } from '../../fields/FieldSymbols';
import { BoolCast, Cast, StrCast } from '../../fields/Types';
import { addStyleSheet, addStyleSheetRule, Utils } from '../../Utils';
import { GoogleAuthenticationManager } from '../apis/GoogleAuthenticationManager';
import { DocServer } from '../DocServer';
import { Networking } from '../Network';
import { MainViewModal } from '../views/MainViewModal';
import { FontIconBox } from '../views/nodes/button/FontIconBox';
import { DragManager } from './DragManager';
import { GroupManager } from './GroupManager';
import './SettingsManager.scss';
import './ReportManager.scss';
import { undoBatch } from './UndoManager';
import { Octokit } from "@octokit/core";
import { CheckBox } from '../views/search/CheckBox';
import ReactLoading from 'react-loading';
import ReactMarkdown from 'react-markdown';
import rehypeRaw from 'rehype-raw';
import remarkGfm from 'remark-gfm';
const higflyout = require('@hig/flyout');
export const { anchorPoints } = higflyout;
export const Flyout = higflyout.default;

@observer
export class ReportManager extends React.Component<{}> {
    public static Instance: ReportManager;
    @observable private isOpen = false;

    private octokit: Octokit;

    @observable public issues: any[] = [];
    @action setIssues = action((issues: any[]) => { this.issues = issues; });
    
    // undefined is the default - null is if the user is making an issue
    @observable public selectedIssue: any = undefined;
    @action setSelectedIssue = action((issue: any) => { this.selectedIssue = issue; });

    // only get the open issues
    @observable public shownIssues = this.issues.filter(issue => issue.state === 'open');
    
    public updateIssueSearch = action((query: string = '') => {
        if (query === '') {
            this.shownIssues = this.issues.filter(issue => issue.state === 'open');
            return;
        }
       this.shownIssues = this.issues.filter(issue => issue.title.toLowerCase().includes(query.toLowerCase()));
    });

    constructor(props: {}) {
        super(props);
        ReportManager.Instance = this;

        this.octokit = new Octokit({
            auth: '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'
        });
    }

    public close = action(() => (this.isOpen = false));
    public open = action(() => {
        if (this.issues.length === 0) {
            // load in the issues if not already loaded
            this.getAllIssues()
                .then(issues => {
                    this.setIssues(issues);
                    this.updateIssueSearch();
                })
                .catch(err => console.log(err));
        }
        (this.isOpen = true)
    });

    private bugTitle = '';
    private bugDescription = '';

    // private toGithub = false;
    // will always be set to true - no alterntive option yet
    private toGithub = true;

    private formatTitle = (title: string, userEmail: string) => `${title} - ${userEmail.replace('@brown.edu', '')}`; 

    public async getAllIssues() : Promise<any[]> {
        const res = await this.octokit.request('GET /repos/{owner}/{repo}/issues', {
            owner: 'brown-dash',
            repo: 'Dash-Web',
        });

        // 200 status means success
        if (res.status === 200) {
            return res.data;
        } else {
            throw new Error('Error getting issues');
        }
    }

    public async reportIssue() {
        if (this.toGithub) {

            const req = await this.octokit.request('POST /repos/{owner}/{repo}/issues', {
                owner: 'brown-dash',
                repo: 'Dash-Web',
                title: this.formatTitle(this.bugTitle, Doc.CurrentUserEmail),
                body: `${this.bugDescription} \n\nfileLinks:\n${(this.fileLinks ?? []).join('\n')}`,
                labels: [
                    'from-dash-app',
                ]
            });

            // 201 status means success
            if (req.status !== 201) {
                alert('Error creating issue on github.');
                // on error, don't close the modal
                return;
            }
        }
        else {
            // if not going to github issues, not sure what to do yet...
        }

        // if we're down here, then we're good to go.
        this.bugTitle = '';
        this.bugDescription = '';
        this.toGithub = false;
        this.setFileLinks([])
        this.close();
    }

    @observable public fileLinks: any = [];
    @action setFileLinks = action((links: any) => { this.fileLinks = links; });

    private getServerPath = (link: any) => { return link.result.accessPaths.agnostic.server }

    private uploadFiles = (input: any) => {
        this.setFileLinks(null);
        // upload the files to the server
        if (input.files && input.files.length !== 0) {
            const fileArray: File[] = Array.from(input.files);
            Networking.UploadFilesToServer(fileArray).then(links => {
                console.log('finshed uploading', links.map(this.getServerPath));
                this.setFileLinks(links.map(this.getServerPath));
            })
        }
        
    }


    private renderIssue = (issue: any) => {

        const isReportingIssue = issue === null;

        return isReportingIssue ?
            // report issue
            (<div className="settings-content">
                <h3 style={{ 'textDecoration': 'underline'}}>Report an Issue</h3>
                <label>Please leave a title for the bug.</label><br />
                <input type="text" placeholder='title' onChange={(e) => this.bugTitle = e.target.value} required/>
                <br />
                <label>Please leave a description for the bug and how it can be recreated.</label>
                <textarea placeholder='description' onChange={(e) => this.bugDescription = e.target.value} required/>
                <br /><br />
                {/* {<label>Send to github issues? </label>
                <input type="checkbox" onChange={(e) => this.toGithub = e.target.checked} />
                <br /> } */}

                <div>
                    <label>Choose file(s) to upload to show the bug (optional)</label>
                    <input type="file" name="file" multiple accept='audio/*, video/*' onChange={e => this.uploadFiles(e.target)}/>
                </div>
                <br />
                    <button onClick={() => this.reportIssue()} disabled={this.fileLinks === null}>{this.fileLinks === null ? 'Uploading...' : 'Submit'}</button>
            </div>)
            :
            // view issue
            (
            <div className='issue-container'>
                <h5 style={{'textAlign': "left"}}><a href={issue.html_url} target="_blank">Issue #{issue.number}</a></h5>
                <div className='issue-title'>
                    {issue.title}
                </div>
                <ReactMarkdown children={issue.body} className='issue-body' linkTarget={"_blank"} remarkPlugins={[remarkGfm]} rehypePlugins={[rehypeRaw]} />
            </div>
        );
    }

    private showReportIssueScreen = () => {
        this.setSelectedIssue(null);
    }

    private closeReportIssueScreen = () => {
        this.setSelectedIssue(undefined);
    }

    private get reportInterface() {

        const isReportingIssue = this.selectedIssue === null;

        return (
            <div className="settings-interface">
                <div className='issue-list-wrapper'>
                    <h3>Current Issues</h3>
                    <input type="text" placeholder='search issues' onChange={(e => this.updateIssueSearch(e.target.value))}></input><br />
                    {this.issues.length === 0 ? <ReactLoading className='loading-center'/> : this.shownIssues.map(issue => <div className='issue-list' key={issue.number} onClick={() => this.setSelectedIssue(issue)}>{issue.title}</div>)}

                    {/* <div className="settings-user">
                        <button onClick={() => this.getAllIssues().then(issues => this.issues = issues)}>Poll Issues</button>
                    </div> */}
                </div>
                    
                <div className="close-button" onClick={this.close}>
                    <FontAwesomeIcon icon={'times'} color="black" size={'lg'} />
                </div>

                <div className="issue-content" style={{'paddingTop' : this.selectedIssue === undefined ? '50px' : 'inherit'}}>
                    {this.selectedIssue === undefined ? "no issue selected" : this.renderIssue(this.selectedIssue)}
                </div>

                <div className='report-issue-fab'>
                    <button
                        onClick={() => isReportingIssue ? this.closeReportIssueScreen() : this.showReportIssueScreen()}
                    >{isReportingIssue ? 'Cancel' : 'Report New Issue'}</button>
                </div>


            </div>
        );
    }

    render() {
        return (
            <MainViewModal
                contents={this.reportInterface}
                isDisplayed={this.isOpen}
                interactive={true}
                closeOnExternalClick={this.close}
                dialogueBoxStyle={{ width: 'auto', height: '500px', background: Cast(Doc.SharingDoc().userColor, 'string', null) }}
            />
        );
    }
}
