using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;

public class MainWindowViewModel : INotifyPropertyChanged
{
    private bool ts_Status;
    public bool Ts_Status
    {
        get { return ts_Status; }
        set
        {
            SetProperty(ref ts_Status, value);
            if (!processError)
            {
                Task.Run(() => Ts_Process(value));
            }
            else
            {
                processError = false;
            }
        }
    }

    private string titleLabelContent;
    public string TitleLabelContent
    {
        get { return titleLabelContent; }
        set { SetProperty(ref titleLabelContent, value); }
    }

    private string label1Content;
    public string Label1Content
    {
        get { return label1Content; }
        set { SetProperty(ref label1Content, value); }
    }

    private string label2Content;
    public string Label2Content
    {
        get { return label2Content; }
        set { SetProperty(ref label2Content, value); }
    }

    private string label4Content;
    public string Label4Content
    {
        get { return label4Content; }
        set { SetProperty(ref label4Content, value); }
    }

    private string label5Content;
    public string Label5Content
    {
        get { return label5Content; }
        set { SetProperty(ref label5Content, value); }
    }

    private string folderPathBoxText;
    public string FolderPathBoxText
    {
        get { return folderPathBoxText; }
        set { SetProperty(ref folderPathBoxText, value); }
    }

    private string button1Content;
    public string Button1Content
    {
        get { return button1Content; }
        set { SetProperty(ref button1Content, value); }
    }

    private string button2Content;
    public string Button2Content
    {
        get { return button2Content; }
        set { SetProperty(ref button2Content, value); }
    }

    private string iconCheckboxContent;
    public string IconCheckboxContent
    {
        get { return iconCheckboxContent; }
        set { SetProperty(ref iconCheckboxContent, value); }
    }

    private string sepaDocCheckboxContent;
    public string SepaDocCheckboxContent
    {
        get { return sepaDocCheckboxContent; }
        set { SetProperty(ref sepaDocCheckboxContent, value); }
    }

    private string freePageCheckboxContent;
    public string FreePageCheckboxContent
    {
        get { return freePageCheckboxContent; }
        set { SetProperty(ref freePageCheckboxContent, value); }
    }

    private string rightClickCheckboxContent;
    public string RightClickCheckboxContent
    {
        get { return rightClickCheckboxContent; }
        set { SetProperty(ref rightClickCheckboxContent, value); }
    }

    private string myPathCheckboxContent;
    public string MyPathCheckboxContent
    {
        get { return myPathCheckboxContent; }
        set { SetProperty(ref myPathCheckboxContent, value); }
    }

    private bool process_Status;
    public bool Process_Status
    {
        get { return process_Status; }
        set { SetProperty(ref process_Status, value); }
    }

    private bool processError = false;

    private bool dummyFlag = false;
    public bool DummyFlag
    {
        get { return dummyFlag; }
        set { SetProperty(ref dummyFlag, value); }
    }

    public async void Ts_Process(bool val)
    {
        if (val)
        {
            bool ret = await Task.Run(() => TsTaskOn());
            if (!ret)
            {
                processError = true;
                Ts_Status = false;
            }
        }
        else
        {
            bool ret = await Task.Run(() => TsTaskOff());
            if (!ret)
            {
                processError = true;
                Ts_Status = true;
            }
        }
    }

    public bool TsTaskOn()
    {
        bool ret = (true && !DummyFlag);

        // 表記を英語に切替
        TitleLabelContent = "Create a template for an AsciiDoc document";
        Label1Content = "output folder";
        Label2Content = "file name";
        Label4Content = "options";
        Label5Content = "format";
        FolderPathBoxText = "<select folder>";
        Button1Content = "browse...";
        Button2Content = "Create";
        IconCheckboxContent = "custom icons";
        SepaDocCheckboxContent = "split document";
        FreePageCheckboxContent = "free page";
        RightClickCheckboxContent = "add shortcut to right-click menu in Explorer (Just once)";
        MyPathCheckboxContent = "saved path";

        if (ret) Process_Status = true;
        return ret;
    }

    public bool TsTaskOff()
    {
        bool ret = (true && !DummyFlag);

        // 表記を日本語に切替
        TitleLabelContent = "AsciiDocの雛形を作成します";
        Label1Content = "作成先のフォルダ:";
        Label2Content = "ドキュメント名:";
        Label4Content = "オプション:";
        Label5Content = "文書フォーマット:";
        FolderPathBoxText = "<フォルダ指定>";
        Button1Content = "参照";
        Button2Content = "作成";
        IconCheckboxContent = "カスタムアイコン";
        SepaDocCheckboxContent = "分割ドキュメント";
        FreePageCheckboxContent = "フリーページ";
        RightClickCheckboxContent = "右クリックショートカット追加(初回のみ)";
        MyPathCheckboxContent = "保存Path";

        if (ret) Process_Status = false;
        return ret;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
    {
        if (Equals(storage, value)) return false;

        storage = value;
        OnPropertyChanged(propertyName);
        return true;
    }

    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}