//pragma solidity >=0.6.10 <0.8.20;
pragma solidity ^0.4.24;
pragma experimental ABIEncoderV2;

import "../Table.sol";
import "./Provider.sol";
import "./Utils.sol";

//阅读记录
contract ReadingRecord {
    event CreateResult(int256 count);
    event InsertResult(int256 count);
    event UpdateResult(int256 count);
    event RemoveResult(int256 count);
    
    
    TableFactory tableFactory = TableFactory(0x1001); 
    Utils utils = new Utils();
    Provider provider = new Provider();
    string constant TABLE_SUFFIX = "_reading_record";
    string constant KEY_NAME = "key";
    string constant KEY_VALUE = "key";
    
    struct DecryptionRecordData{
        string  id;
        string  weid;
        string  sm9Hibe_id;
        int256  content_type;
        string  content_id;
        int256  timestamp;
        int256  duration;
        string  location;
        int256  create_time;
        string  sign;
    }
    
    //根据id查询记录
    function select(string memory table_prefix,string memory id)
    public
    view
    returns (DecryptionRecordData[]){
         //确定表格名称
        string memory table_name = string(abi.encodePacked(table_prefix, TABLE_SUFFIX));
        Table table = tableFactory.openTable(table_name);

        Condition condition = table.newCondition();
        condition.EQ("id", id);
        
        Entries entries = table.select(KEY_VALUE, condition);
         uint256  dataLen =   uint256(entries.size());
        DecryptionRecordData[] memory recordDatas =  new DecryptionRecordData[](dataLen);
        
        for (uint256 i = 0; i < dataLen; ++i) {
            Entry entry = entries.get(int256(i));
            recordDatas[i]=DecryptionRecordData(entry.getString("id"),entry.getString("weid"),entry.getString("sm9Hibe_id"),entry.getInt("content_type"),entry.getString("content_id"),entry.getInt("timestamp"),entry.getInt("duration"),entry.getString("location"),entry.getInt("create_time"),entry.getString("sign"));
        }

        return (recordDatas);
    }
    
    //添加下载记录,临时用数组方式传入int类型变量规避堆栈过深
    function insert(string memory id, string[] memory str_attr_name,string[] memory str_attr_value,string[] memory int_attr_name,int256[] memory int_attr_value)
    public 
    returns(int256,string memory){
        //表格前缀
        string memory table_prefix = provider.selectPrefixByAddr(utils.toString(tx.origin));
        if(utils.strIsEmpty(table_prefix)){
            return (0,"table_prefix not find");
        }
        
        //确定表格名称
        string memory table_name = string(abi.encodePacked(table_prefix, TABLE_SUFFIX));
        Table table = tableFactory.openTable(table_name);
         //id重复校验
        if(existById(id,table)){
            return (0,"id exists,insert failed");
        }
        Entry entry = table.newEntry();
        entry.set(KEY_NAME, KEY_VALUE);
        entry.set("id", id);
        
        //保存str类型属性
         for (uint256 i = 0; i < str_attr_name.length; ++i) {
             entry.set(str_attr_name[i],str_attr_value[i]);
         }
        
        //保存int类型属性
        for ( i = 0; i < int_attr_name.length; ++i) {
             entry.set(int_attr_name[i],int_attr_value[i]);
         }

        int256 count = table.insert(KEY_VALUE, entry);
        if(count>0){
            emit InsertResult(count);
        }
        return (count,"success");
        
    }
    
    //根据id判定是否存在
    function existById(string memory id,Table table)
    private
    view
    returns (bool){
        Condition condition = table.newCondition();
        condition.EQ("id", id);
        Entries entries = table.select(KEY_VALUE, condition);
        return entries.size()>0;
    }
    
}