<commit_before>//
//  MovieUserInfoViewController.swift
//  FiNote
//
//  Created by 岩見建汰 on 2018/01/26.
//  Copyright © 2018年 Kenta. All rights reserved.
//

import UIKit
import Eureka
import NVActivityIndicatorView
import Alamofire
import SwiftyJSON
import KeychainAccess

class MovieUserInfoViewController: FormViewController {

    var movie_id = ""
    var onomatopoeia: [String] = []
    var dvd = false
    var fav = false
    var count = 1
    var choices: [String] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.title = "Edit Info"
        
        let close = UIBarButtonItem(image: UIImage(named: "icon_cancel"), style: .plain, target: self, action: #selector(TapCloseButton))
        let save = UIBarButtonItem(image: UIImage(named: "icon_check"), style: .plain, target: self, action: #selector(TapSaveButton))
        self.navigationItem.setLeftBarButton(close, animated: true)
        self.navigationItem.setRightBarButton(save, animated: true)
        
        CallGetOnomatopoeiaAPI()
    }
    
    func TapCloseButton() {
        self.dismiss(animated: true, completion: nil)
    }
    
    func TapSaveButton() {
        let choosing = GetChoosingOnomatopoeia()
        
        if choosing.count == 0 {
            ShowStandardAlert(title: "Error", msg: "オノマトペは少なくとも1つ以上追加する必要があります", vc: self)
        }else {
            CallUpdateMovieUserInfoAPI()
        }
    }
    
    func SetMovieID(movie_id: String) {
        self.movie_id = movie_id
    }
    
    func SetOnomatopoeia(onomatopoeia: [String]) {
        self.onomatopoeia = onomatopoeia
    }
    
    func SetDVD(dvd: Bool) {
        self.dvd = dvd
    }
    
    func SetFAV(fav: Bool) {
        self.fav = fav
    }
    
    func CreateForm() {
        UIView.setAnimationsEnabled(false)
        
        form +++ Section(header: "DVDの所持・お気に入り登録", footer: "")
            <<< SwitchRow("") { row in
                row.title = "DVD"
                row.value = dvd
                row.tag = "dvd"
            }
        
            <<< SwitchRow("") { row in
                row.title = "Favorite"
                row.value = fav
                row.tag = "fav"
            }
        
        
        form +++ MultivaluedSection(multivaluedOptions: [.Insert, .Delete],
                           header: "オノマトペの登録",
                           footer: "映画を観た気分を登録してください") {
                            $0.tag = "onomatopoeia"
                            $0.multivaluedRowToInsertAt = { _ in
                                return PickerInputRow<String>{
                                    let options = self.GetOnomatopoeiaNewChoices()
                                    $0.title = "タップして選択..."
                                    $0.options = options
                                    //TODO: fix
                                    $0.value = options.first!
                                    $0.tag = "onomatopoeia_\(self.count)"
                                    self.count += 1
                                }
                            }
                            
                            for (i, name) in onomatopoeia.enumerated() {
                                $0 <<< PickerInputRow<String> {
                                    $0.title = "タップして選択..."
                                    $0.value = name
                                    $0.options = self.GetOnomatopoeiaNewChoices()
                                    $0.tag = "onomatopoeia_\(i)"
                                }
                                count = i+1
                            }
        }
        
        UIView.setAnimationsEnabled(true)
    }
    
    func CallGetOnomatopoeiaAPI() {
        let urlString = API.base.rawValue+API.v1.rawValue+API.onomatopoeia.rawValue+API.choice.rawValue
        let activityData = ActivityData(message: "Get Onomatopoeia", type: .lineScaleParty)
        NVActivityIndicatorPresenter.sharedInstance.startAnimating(activityData)
        
        DispatchQueue(label: "get-onomatopoeia").async {
            
            Alamofire.request(urlString, method: .get).responseJSON { (response) in
                NVActivityIndicatorPresenter.sharedInstance.stopAnimating()
                
                guard let res = response.result.value else{return}
                let obj = JSON(res)
                print("***** API results *****")
                print(obj)
                print("***** API results *****")
                
                if IsHTTPStatus(statusCode: response.response?.statusCode) {
                    self.choices = obj["results"].arrayValue.map{$0.stringValue}<commit_msg>セルを選択した時にoptionsを更新<commit_after>//
//  MovieUserInfoViewController.swift
//  FiNote
//
//  Created by 岩見建汰 on 2018/01/26.
//  Copyright © 2018年 Kenta. All rights reserved.
//

import UIKit
import Eureka
import NVActivityIndicatorView
import Alamofire
import SwiftyJSON
import KeychainAccess

class MovieUserInfoViewController: FormViewController {

    var movie_id = ""
    var onomatopoeia: [String] = []
    var dvd = false
    var fav = false
    var count = 1
    var choices: [String] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.title = "Edit Info"
        
        let close = UIBarButtonItem(image: UIImage(named: "icon_cancel"), style: .plain, target: self, action: #selector(TapCloseButton))
        let save = UIBarButtonItem(image: UIImage(named: "icon_check"), style: .plain, target: self, action: #selector(TapSaveButton))
        self.navigationItem.setLeftBarButton(close, animated: true)
        self.navigationItem.setRightBarButton(save, animated: true)
        
        CallGetOnomatopoeiaAPI()
    }
    
    func TapCloseButton() {
        self.dismiss(animated: true, completion: nil)
    }
    
    func TapSaveButton() {
        let choosing = GetChoosingOnomatopoeia()
        
        if choosing.count == 0 {
            ShowStandardAlert(title: "Error", msg: "オノマトペは少なくとも1つ以上追加する必要があります", vc: self)
        }else {
            CallUpdateMovieUserInfoAPI()
        }
    }
    
    func SetMovieID(movie_id: String) {
        self.movie_id = movie_id
    }
    
    func SetOnomatopoeia(onomatopoeia: [String]) {
        self.onomatopoeia = onomatopoeia
    }
    
    func SetDVD(dvd: Bool) {
        self.dvd = dvd
    }
    
    func SetFAV(fav: Bool) {
        self.fav = fav
    }
    
    func CreateForm() {
        UIView.setAnimationsEnabled(false)
        
        form +++ Section(header: "DVDの所持・お気に入り登録", footer: "")
            <<< SwitchRow("") { row in
                row.title = "DVD"
                row.value = dvd
                row.tag = "dvd"
            }
        
            <<< SwitchRow("") { row in
                row.title = "Favorite"
                row.value = fav
                row.tag = "fav"
            }
        
        
        form +++ MultivaluedSection(multivaluedOptions: [.Insert, .Delete],
                           header: "オノマトペの登録",
                           footer: "映画を観た気分を登録してください") {
                            $0.tag = "onomatopoeia"
                            $0.multivaluedRowToInsertAt = { _ in
                                let options = self.GetOnomatopoeiaNewChoices()
                                
                                if options.count == 0 {
                                    return PickerInputRow<String>{
                                        $0.title = "タップして選択..."
                                        $0.options = options
                                        $0.value = ""
                                        $0.tag = "onomatopoeia_\(self.count)"
                                        self.count += 1
                                    }.onCellSelection({ (cell, row) in
                                        row.options = self.GetOnomatopoeiaNewChoices()
                                    })
                                }else {
                                    return PickerInputRow<String>{
                                        $0.title = "タップして選択..."
                                        $0.options = options
                                        $0.value = options.first!
                                        $0.tag = "onomatopoeia_\(self.count)"
                                        self.count += 1
                                    }.onCellSelection({ (cell, row) in
                                        row.options = self.GetOnomatopoeiaNewChoices()
                                    })
                                }
                            }
                            
                            for (i, name) in onomatopoeia.enumerated() {
                                $0 <<< PickerInputRow<String> {
                                    $0.title = "タップして選択..."
                                    $0.value = name
                                    $0.options = self.GetOnomatopoeiaNewChoices()
                                    $0.tag = "onomatopoeia_\(i)"
                                    }.onCellSelection({ (cell, row) in
                                        //TODO: 自分は含める
                                        row.options = self.GetOnomatopoeiaNewChoices()
                                    })
                                count = i+1
                            }
        }
        
        UIView.setAnimationsEnabled(true)
    }
    
    func CallGetOnomatopoeiaAPI() {
        let urlString = API.base.rawValue+API.v1.rawValue+API.onomatopoeia.rawValue+API.choice.rawValue
        let activityData = ActivityData(message: "Get Onomatopoeia", type: .lineScaleParty)
        NVActivityIndicatorPresenter.sharedInstance.startAnimating(activityData)
        
        DispatchQueue(label: "get-onomatopoeia").async {
            
            Alamofire.request(urlString, method: .get).responseJSON { (response) in
                NVActivityIndicatorPresenter.sharedInstance.stopAnimating()
                
                guard let res = response.result.value else{return}
                let obj = JSON(res)
                print("***** API results *****")
                print(obj)
                print("***** API results *****")
                
                if IsHTTPStatus(statusCode: response.response?.statusCode) {
                    self.choices = obj["results"].arrayValue.map{$0.stringValue}
{'fraction_non_alphanumeric': 0.09467342450033471, 'fraction_numerical': 0.006311561633355647, 'mean_word_length': 1.2199108469539377, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 43, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6099541', 'n_tokens_mistral': 2980, 'n_tokens_neox': 2840, 'n_words': 508}